package com.dgtly.wxportal.utils.file;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @description:下载文件
 * @author:qxm
 * @date:2020/10/20 15:14
 */
public class fileDownloadUtil {

    public static void downloadFile(String filePath,String fileName,String httpUrl){
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection =  (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();

            bis = new BufferedInputStream(is);

            File file = new File(filePath+"/"+fileName);//E:\test.pdf
            if (!file.getParentFile().exists())
            {
                file.getParentFile().mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            int b = 0;
            byte[] byArr = new byte[1024*4];
            while((b=bis.read(byArr))!=-1){
                bos.write(byArr, 0, b);
            }

//            FileInputStream inputStream = new FileInputStream(file);
//            MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(is));
//            String uploadPath = FileUploadUtils.upload(Global.getPdfPath(), multipartFile);
//            return uploadPath;

        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(bis!=null){
                    bis.close();
                }
                if(bos!=null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
