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;
           }
}

 

FileUtil 里面提前关闭流了