【Java 文件下载工具类】

FileUtils

package com.cmbchina.ccd.itpm.project.util;
 
import org.slf4j.Logger;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
 
import static org.slf4j.LoggerFactory.getLogger;
 
/**
 * @ClassName FileUtils
 * @Description 文件下载
 **/
public class FileUtils {
 
    private static final Logger logger = getLogger(FileUtils.class);
 
    public static void download(HttpServletResponse response, String filePath, String encode) {
        response.setContentType("text/html;charset=" + encode);
        String downLoadPath = filePath;
        File file = new File(downLoadPath);
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(downLoadPath)); BufferedOutputStream bos = new BufferedOutputStream(
                response.getOutputStream())) {
            long fileLength = file.length();
            String fileName = file.getName();
            response.setContentType("application/x-msdownload;");
            response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(encode), StandardCharsets.UTF_8));
            response.setHeader("Content-Length", String.valueOf(fileLength));
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
 
    /**
     * 以流的方式下载
     *
     * @param path
     * @param response
     * @return
     */
    public HttpServletResponse download(String path, HttpServletResponse response) {
        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(".".lastIndexOf(filename) + 1).toUpperCase();
            // 以流的形式下载文件。
            byte[] buffer;
            try (InputStream fis = new BufferedInputStream(new FileInputStream(path))) {
                buffer = new byte[fis.available()];
                fis.read(buffer);
            }
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            logger.error("{}", ex);
        }
        return response;
    }
 
    /**
     * 下载本地文件
     *
     * @param response
     * @throws FileNotFoundException
     */
    public void downloadLocal(HttpServletResponse response) throws IOException {
        // 下载本地文件
        // 文件的默认保存名
        String fileName = "Operator.doc";
        // 读到流中
        // 文件的存放路径
        try (InputStream inStream = new FileInputStream("c:/Operator.doc")) {
            // 设置输出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            // 循环取出流中的数据
            byte[] b = new byte[100];
            int len;
            try {
                while ((len = inStream.read(b)) > 0) {
                    response.getOutputStream().write(b, 0, len);
                }
            } catch (IOException e) {
                logger.error("{}", e);
            }
        }
 
    }
 
    /**
     * 下载网络文件
     *
     * @throws IOException
     */
    public void downloadNet() throws IOException {
        // 下载网络文件
        int bytesum = 0;
        int byteread;
 
        URL url = new URL("windine.blogdriver.com/logo.gif");
 
        URLConnection conn = url.openConnection();
        try (InputStream inStream = conn.getInputStream(); FileOutputStream fs = new FileOutputStream("c:/abc.gif")) {
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
        }
    }
 
 
    public void downLoadIsOnLine(String fileName, HttpServletRequest request, HttpServletResponse response, boolean isOnLine) throws IOException {
        String absolutePath = fileName;
        File f = new File(absolutePath);
        if (!f.exists()) {
            response.sendError(404, "File not found!");
            return;
        }
        //解决中文乱码
        String userAgent = request.getHeader("user-agent").toLowerCase();
        fileName = f.getName();
        String downloadFileName;
 
        if (userAgent.contains("msie") || userAgent.contains("like gecko")) {
            downloadFileName = URLEncoder.encode((fileName), "UTF-8");
        } else {
            downloadFileName = new String((fileName).getBytes(StandardCharsets.UTF_8), "iso-8859-1");
        }
        byte[] buf = new byte[1024];
        int len = 0;
 
        response.reset(); // 非常重要
        // 在线打开方式
        if (isOnLine) {
            URL u = new URL("file:///" + absolutePath);
            response.setContentType(u.openConnection().getContentType());
            response.setHeader("Content-Disposition", "inline; filename=" + downloadFileName);
            // 文件名应该编码成UTF-8
        } else { // 纯下载方式
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename=" + downloadFileName);
        }
 
        try (BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); OutputStream out = response.getOutputStream()) {
 
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    }
 
 
}

简单使用

 /**
     * 下载文件
     */
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) throws IOException {
        ObjectStat stat = minIoUtil.statObject("", fileName);
        InputStream in = minIoUtil.getObject("", fileName);
        int index;
        byte[] bytes = new byte[1024];
        File filePath = new File(localhostPath);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }

        FileOutputStream downloadFile = new FileOutputStream(localhostPath+ "\\" + fileName);
        while ((index = in.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        in.close();
        downloadFile.close();
        File file = new File(localhostPath+ "\\" + fileName);
        if (file.exists() && file.isFile()) {
            responseFile(response,localhostPath+ "\\" + fileName);
            file.delete();
        }

    }

/**
*文件读取
*/
public void responseFile(HttpServletResponse response, String path) {
        try {
            byte[] b = FileUtils.readFileToByteArray(new File(path));
            if (b == null) {
                return;
            }
            System.out.println("获取流" + b);
            OutputStream out = response.getOutputStream();
            out.write(b);
            out.flush();
            out.close();
        } catch (Exception var5) {
            System.out.println("当前路径文件读取失败:" + path + var5);
            var5.printStackTrace();
        }

    }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值