wanzhitao18 2021-02-20 17:34 采纳率: 50%
浏览 336
已结题

feign远程调用下载文件问题?

2021-02-20 17:29:09.289  INFO 15460 --- [nio-8091-exec-8] c.miju.render.service.RenderTaskService  : 下载渲染文件异常[org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。],[java.io.IOException: 你的主机中的软件中止了一个已建立的连接。]
public Result<String> dowFiles(String taskUid, HttpServletResponse response)throws IOException{
        //服务器路径
        String filePath = pathProperties.getShareTaskPath()+"\\"+taskUid;
        //批量下载文件 zip压缩
        String zipName = taskUid+".zip";
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition","attachment; filename="+zipName);
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {
            FileUtil.doCompress(filePath, out);
            response.flushBuffer();
        } catch (Exception e) {
            log.info("下载渲染文件异常[{}],[{}]",e.toString(),e.getMessage());
            return resultUtil.error(ResponseCodeMessage.HTTP_CLIENT_ERROR);
        }finally{
            out.close();
            return null;
        }
    }






package com.miju.common.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件处理
 */
public class FileUtil {


    public static void doCompress(String srcFile, String zipFile) throws IOException {
        doCompress(new File(srcFile), new File(zipFile));
    }

    /**
     * 文件压缩
     * @param srcFile 目录或者单个文件
     * @param zipFile 压缩后的ZIP文件
     */
    public static void doCompress(File srcFile, File zipFile) throws IOException {
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipFile));
            doCompress(srcFile, out);
        } catch (Exception e) {
            throw e;
        } finally {
            out.close();//记得关闭资源
        }
    }

    public static void doCompress(String filelName, ZipOutputStream out) throws IOException{
        doCompress(new File(filelName), out);
    }

    public static void doCompress(File file, ZipOutputStream out) throws IOException{
        doCompress(file, out, "");
    }

    public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException {
        if ( inFile.isDirectory() ) {
            File[] files = inFile.listFiles();
            if (files!=null && files.length>0) {
                for (File file : files) {
                    String name = inFile.getName();
                    if (!"".equals(dir)) {
                        name = dir + "/" + name;
                    }
                    FileUtil.doCompress(file, out, name);
                }
            }
        } else {
            FileUtil.doZip(inFile, out, dir);
        }
    }

    public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException {
        String entryName = null;
        if (!"".equals(dir)) {
            entryName = dir + "/" + inFile.getName();
        } else {
            entryName = inFile.getName();
        }
        ZipEntry entry = new ZipEntry(entryName);
        out.putNextEntry(entry);

        int len = 0 ;
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(inFile);
        while ((len = fis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
            out.flush();
        }
        out.closeEntry();
        fis.close();
    }

    /**
     * 下载单个文件
     * @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(filename.lastIndexOf(".") + 1).toUpperCase();
               // 以流的形式下载文件。
               InputStream fis = new BufferedInputStream(new FileInputStream(path));
               byte[] buffer = new byte[fis.available()];
               fis.read(buffer);
               fis.close();
               // 清空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) {
               ex.printStackTrace();
           }
            return response;
           }
}
  • 写回答

1条回答 默认 最新

  • lin351550660 2021-02-21 16:50
    关注

    FileUtil 里面提前关闭流了  

    评论

报告相同问题?

悬赏问题

  • ¥15 程序实在不会写,要秃了
  • ¥15 pycharm导入不了自己的包
  • ¥15 C#.net通过内网url地址获取文件并下载问题,浏览器postman可以正常下载,用程序不行
  • ¥15 本人本科机械,目前研一。没有深度学习基础,目前对研究生课题一片迷茫,请教各位!
  • ¥15 关于R语言单因素与多因素线性回归的平均值
  • ¥15 服务器清除BIOS之后引导不了
  • ¥15 CPLEX用OPL编写的混合整数线性优化问题。
  • ¥15 可以用EasyConnect连接实验室内网,但无法连接内网才能访问的服务器,为什么?
  • ¥15 前端预览docx文件,文件从后端传送过来。
  • ¥15 层次聚类和蛋白质相似度