其他分享
首页 > 其他分享> > 下载工具类

下载工具类

作者:互联网

package com.hisense.cis.test;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

public class Down2 {
    public static  void  httpDownload(HttpServletRequest request, HttpServletResponse response,String httpUrl) {
        System.out.println("下载"+httpUrl);
        // 1.下载网络文件
        ServletOutputStream out = null;        int byteRead;
        URL url=null;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
           // return false;
        }
        try {
            //2.获取链接
            URLConnection conn =url.openConnection();            //3.输入流
            InputStream inStream = conn.getInputStream();            String downloadfilename=null;
            //3.写入文件image/jpeg
            if(httpUrl.contains("ImageViewServlet")){               // response.setContentType("image/jpeg");
                response.setCharacterEncoding("UTF-8");                String agent = request.getHeader("User-Agent");
                downloadfilename=LocalDateTime.now().toLocalDate()+"";
                response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadfilename+".png" + "\"");
            }else{
                response.setCharacterEncoding("UTF-8");
                String agent = request.getHeader("User-Agent");
                boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
                 downloadfilename=httpUrl.substring(httpUrl.indexOf("downloadcenter")+16,httpUrl.length());
                System.out.println(">>>>downloadfilename>>>>>"+downloadfilename);
                if (isMSIE) {
                    downloadfilename = java.net.URLEncoder.encode(downloadfilename, "UTF8");
                } else {
                    downloadfilename = new String(downloadfilename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
                }
                response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadfilename + "\"");
            }
            System.out.println("下载"+httpUrl+"文件"+downloadfilename);
           // response.setContentType("multipart/form-data");
            out = response.getOutputStream();            //读取文件流
            int len = 0;            byte[] buffer = new byte[1024 * 10];
            while ((len = inStream.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
            inStream.close();
           // return true;
        } catch (FileNotFoundException e) {            e.printStackTrace();
           // return false;
        } catch (IOException e) {            e.printStackTrace();
           // return false;
        }    }

    public static void main(String[] args) {
       // System.out.println(httpDownload("ftp://ygdy8:ygdy8@yg05.dydytt.net:1001/%E9%98%B3%E5%85%89%E7%94%B5%E5%BD%B1www.ygdy8.com.%E5%85%AB%E4%BD%B0.HD.1080p.%E5%9B%BD%E8%AF%AD%E4%B8%AD%E8%8B%B1%E5%8F%8C%E5%AD%97.mp4", "D:\\22.mp4"));

       // System.out.println(httpDownload("http://tms.hisense.com/ImageViewServlet?id=1373358", "D:\\1.jpg"));
String str="http://minio-hisense-pangea-2-prd.prdapp.hisense.com/downloadcenter/%E8%AE%A4%E8%B4%AD%E5%8F%82%E4%B8%8E%E7%BB%93%E6%9E%9C%2820210202-093030%29.xlsx";        //System.out.println(httpDownload(str, "D:\\1.xls"));

    }}

标签:下载工具,java,downloadfilename,httpUrl,import,response,out
来源: https://blog.csdn.net/weixin_41722928/article/details/113550142