下载文件(二)java HttpURLConnection下载文件

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报错。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中使用HttpURLConnection上传文件可以通过以下步骤实现: 1. 创建URL对象,指定上传文件的URL地址。 2. 打开HTTP连接,并设置请求方法为POST。 3. 设置HTTP请求头部信息,包括Content-Type和boundary。 4. 设置HTTP请求体信息,包括上传文件的内容和分割线。 5. 读取服务器端返回的响应信息。 示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws Exception { String urlStr = "http://example.com/upload"; String filePath = "C:/test.txt"; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); String boundary = "---------------------------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream out = conn.getOutputStream(); // 写入分割线 out.write(("--" + boundary + "\r\n").getBytes()); // 写入文件名 out.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath + "\"\r\n").getBytes()); // 写入文件类型 out.write(("Content-Type: application/octet-stream\r\n").getBytes()); // 写入空行 out.write(("\r\n").getBytes()); // 写入文件内容 FileInputStream fileIn = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int len; while ((len = fileIn.read(buffer)) != -1) { out.write(buffer, 0, len); } fileIn.close(); // 写入空行 out.write(("\r\n").getBytes()); // 写入分割线 out.write(("--" + boundary + "--\r\n").getBytes()); out.flush(); out.close(); // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 其中,filePath为需要上传的文件路径,urlStr为服务器端接收文件的URL地址。在示例代码中,我们使用了multipart/form-data类型的请求体格式,可以在请求头部信息中设置Content-Disposition、Content-Type等参数。同时,我们也设置了一个分割线boundary,用于分隔不同的请求体部分。最后,我们通过读取服务器端返回的响应信息,来获取上传文件的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值