1、忽略HTTPS请求的SSL证书
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class SslUtils {
public static void trustAllHttpsCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[1];
TrustManager tm = new miTM();
trustAllCerts[0] = tm;
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
static class miTM implements TrustManager,X509TrustManager {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public boolean isServerTrusted(X509Certificate[] certs) {
return true;
}
public boolean isClientTrusted(X509Certificate[] certs) {
return true;
}
public void checkServerTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
public void checkClientTrusted(X509Certificate[] certs, String authType)
throws CertificateException {
return;
}
}
/**
* 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
* @throws Exception
*/
public static void ignoreSsl() throws Exception{
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
}
2、下载文件util:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpURLConnectionUtil {
private static Logger logger = LoggerFactory.getLogger(HttpURLConnectionUtil.class);
/**
* 通过url下载文件到指定文件夹
* @param downloadUrl url
* @param downloadDir 目标目录
*/
public static void downloadFile(String downloadDir,String downloadUrl) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
try {
SslUtils.ignoreSsl();
} catch (Exception e) {
e.printStackTrace();
}
String docname = downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1);
downloadUrl = downloadUrl.substring(0,downloadUrl .lastIndexOf("/"))+"/"+URLEncoder.encode(docname,"utf-8");
//获取连接
URL url=new URL(downloadUrl);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(4*1000) ;
connection.setRequestMethod("GET") ;
connection.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +
"application/x-shockwave-flash, application/xaml+xml, " +
"application/vnd.ms-xpsdocument, application/x-ms-xbap, " +
"application/x-ms-application, application/vnd.ms-excel, " +
"application/vnd.ms-powerpoint, application/msword, */*");
connection.setRequestProperty("Accept-Language", "zh-CN");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//设置浏览器类型和版本、操作系统,使用语言等信息
connection.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; " +
".NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; " +
".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
//设置为长连接
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Accept", "*/*");
//获取输入流
inputStream=connection.getInputStream();
File fileDir=new File(downloadDir);
if(!fileDir.exists()){//如果文件夹不存在
fileDir.mkdirs();//创建文件夹
}
//截取文件名称,可以把 / 换成需要的规则
String filePath = downloadDir+File.separator + docname;
File file = new File(filePath);
file.createNewFile();//创建文件,存在覆盖
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
connection.disconnect();
} catch (Exception e) {
logger.error("文件下载出错:" + e);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
logger.error("关闭流出错:" + e);
}
}
}
/**
* 压缩并下载压缩包
*/
/*public static void downloadZip(HttpServletRequest request, HttpServletResponse res,
String downloadDir,
String downloadZip, String zipName) throws IOException {
OutputStream out = null;
File zip = null;
try{
//多个文件进行压缩,批量打包下载文件
//创建压缩文件需要的空的zip包
String zipFilePath = downloadZip+File.separator+zipName;
File fileDir=new File(downloadZip);
if(!fileDir.exists()){//如果文件夹不存在
fileDir.mkdir();//创建文件夹
}
//压缩文件
zip = new File(zipFilePath);
zip.createNewFile();//创建文件,存在覆盖
//创建zip文件输出流
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
zipFile(downloadDir, zos);
zos.close();
//将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
byte[] buff = new byte[bis.available()];
bis.read(buff);
bis.close();
//IO流实现下载的功能
res.setCharacterEncoding("UTF-8"); //设置编码字符
res.setContentType("application/octet-stream;charset=UTF-8"); //设置下载内容类型application/octet-stream(二进制流,未知文件类型);
//防止文件名乱码
String userAgent = request.getHeader("USER-AGENT");
if (userAgent.contains("Firefox") || userAgent.contains("firefox")) {//火狐浏览器
zipName = new String(zipName.getBytes(), "ISO8859-1");
} else {
zipName = URLEncoder.encode(zipName, "UTF-8");//其他浏览器
}
res.setHeader("Content-disposition", "attachment;filename="+zipName);//设置下载的压缩文件名称
out = res.getOutputStream(); //创建页面返回方式为输出流,会自动弹出下载框
out.write(buff);//输出数据文件
}catch(Exception e) {
logger.error("压缩包下载出错:" + e);
}finally {
if(out != null){
out.flush();//释放缓存
out.close();//关闭输出流
}
//下载完后删除文件夹和压缩包
File fileDir = new File(downloadDir);
FileUtils.deleteDirectory(fileDir);
if(zip != null){
zip.delete();
}
}
}*/
/**
* 压缩文件
* @param filePath 需要压缩的文件夹
* @param zos zip文件输出流
*/
/* private static void zipFile(String filePath,ZipOutputStream zos) throws IOException {
File inputFile = new File(filePath); //根据文件路径创建文件
if(inputFile.exists()) { //判断文件是否存在
if (inputFile.isFile()) { //判断是否属于文件,还是文件夹
//创建输入流读取文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile));
//将文件写入zip内,即将文件进行打包
zos.putNextEntry(new ZipEntry(inputFile.getName()));
//写入文件的方法,同上
int size = 0;
byte[] buffer = new byte[1024]; //设置读取数据缓存大小
while ((size = bis.read(buffer)) > 0) {
zos.write(buffer, 0, size);
}
//关闭输入输出流
zos.closeEntry();
bis.close();
} else { //如果是文件夹,则使用穷举的方法获取文件,写入zip
try {
File[] files = inputFile.listFiles();
for (File fileTem:files) {
zipFile(fileTem.toString(),zos);
}
} catch (Exception e) {
logger.error("压缩文件出错:" + e);
}
}
}
}*/
}
说明:
1)SslUtils.ignoreSsl();防止https请求失败,解决javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException报错
2)downloadUrl = downloadUrl.substring(0,downloadUrl .lastIndexOf("/"))+"/"+URLEncoder.encode(docname,"utf-8");
对文件名进行编码,因为HttpURLConnection无法打开含有中文的链接,解决文件路径404报错。
|