FileUtil 工具类

package com.jiayou.peis.report.biz.utils;

import lombok.SneakyThrows;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Base64;

public class FileUtil {
    /**
     * 根据URL下载文件(pdf/图片)并转换为Base64
     * @param fileUrl
     * @return
     */
    public static String transUrlToBase64(String fileUrl){
        try {
            URL url = new URL(fileUrl);
            InputStream inputStream = url.openStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream,outputStream);
            byte[] byteArray = outputStream.toByteArray();
            return Base64.getEncoder().encodeToString(byteArray);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 下载文件
     *
     * @param filePath 文件路径
     * @param response 响应体
     */
    @SneakyThrows
    public static void downloadFile(String filePath, HttpServletResponse response) {
        File file = new File(filePath);
        //输入流,读取文件内容
        FileInputStream fileInputStream = new FileInputStream(file.getPath());
        //输出流,将文件写回浏览器
        ServletOutputStream servletOutputStream = response.getOutputStream();
        //响应格式(图片)
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes()));
        response.addHeader("Content-Length", String.valueOf(file.length()));
        response.setContentType("application/octet-stream");
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fileInputStream.read(bytes)) != -1) {
            servletOutputStream.write(bytes, 0, len);
            servletOutputStream.flush();
        }
        fileInputStream.close();
        servletOutputStream.close();
    }

    /**
     * 下载远程文件并保存到本地
     *
     * @param remoteFilePath-远程文件路径
     * @param localFilePath-本地文件路径(带文件名)
     */
    public static void downloadFile1(String remoteFilePath, String localFilePath) {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        try {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection) urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 下载远程文件并保存到本地
     *
     * @param remoteFilePath-远程文件路径
     * @param localFilePath-本地文件路径(带文件名)
     */
    public static void downloadFile2(String remoteFilePath, String localFilePath) {
        URL website = null;
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {
            website = new URL(remoteFilePath);
            rbc = Channels.newChannel(website.openStream());
            fos = new FileOutputStream(localFilePath);//本地要存储的文件地址 例如:test.txt
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(rbc!=null){
                try {
                    rbc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    /**
     * 小试牛刀
     * @param args
     */
    public static void main(String[] args) {
        /*远程文件路径*/
        String remoteFilePath1 = "http://192.168.150.190:9000/reportfile/20230727/TJH24289_1.pdf";
        //String remoteFilePath2 = "https://pic.3gbizhi.com/2019/1112/20191112013312648.jpg";
        /*本地文件路径(带文件名)*/
        String localFilePath1 ="C:\\Users\\Administrator\\Desktop\\biji\\广州塔.pdf";
        //String localFilePath2 ="E:\\LeStoreDownload\\update\\大桥.jpg";
        downloadFile1(remoteFilePath1,localFilePath1);
        //downloadFile2(remoteFilePath2,localFilePath2);
    }



}

你可能感兴趣的:(功能模块,java,笔记)