xihawuyou 2015-09-01 08:25 采纳率: 0%
浏览 2811

java下载zip文件socket write error

今天用java下载zip文件的时候老是出现如下错误,从网上找了错误原因,感觉答案解决不了我的问题:

ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:413)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:342)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:438)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:426)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)

代码如下:
File file = new File("d:/zhangrm/aa/test.zip");
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(file.getName(), "UTF-8"))));
response.setContentType("application/x-msdownload");
response.setContentLength((int) file.length());

    try {

        InputStream inputStream = new FileInputStream(file);
        BufferedInputStream buffInput = new BufferedInputStream(inputStream);

        //写文件  
        byte[] b = new byte[1024];
        long k = 0;

        OutputStream outputStream = response.getOutputStream();
        BufferedOutputStream buffOutput=new BufferedOutputStream(outputStream);

         while (k < file.length()) {
             int j = buffInput.read(b, 0, 1024);
             k += j;
             buffOutput.write(b, 0, j);
         }
        buffOutput.flush();
        inputStream.close();
        buffInput.close();
        outputStream.close();
        buffOutput.close();

       // deleteFile(pathTo);
        //deleteFile(Const.ZIPFILENAME);

    } catch (IOException e) {
        log.error("数据下载失败!");
        e.printStackTrace();
    }

    望大神帮忙解决下!!!小妹不胜感激!
  • 写回答

1条回答 默认 最新

  • 天涯云海 2015-09-01 09:11
    关注
    BufferedInputStream buffInput = null;
        BufferedOutputStream buffOutput = null;
        try {
            buffInput = new BufferedInputStream(new FileInputStream(file));
            // 写文件
            byte[] b = new byte[1024];
            int k = -1;
            buffOutput = new BufferedOutputStream(response.getOutputStream()));
            while ((k = buffInput.read(b)) != -1) {
                buffOutput.write(b, 0, k);
            }
            buffOutput.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (buffOutput != null) {
                    buffOutput.close();
                }
                if (buffInput != null) {
                    buffInput.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?