web大文件下载,用户点击取消报错
下面是我java后台下的文件下载代码
用户点击弹出下载框的取消按钮时报错(java.net.SocketException: Software caused connection abort: socket write error)

@RequestMapping(value = "download")
public void download(CareUpload careUpload , HttpServletRequest request, HttpServletResponse response){
    String fileSavename = careUpload.getFileSavename();//服务器存储文件名
    String path = careUpload.getPath();                //服务器存储路径
    String fileRealname = careUpload.getFileRealname();//服务器存储文件上传前名称

    String realPath = request.getSession().getServletContext().getRealPath(File.separator);


    String filePath = path + File.separator + fileSavename;

    File file = new File(filePath);

    if (file.exists()) {
        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset();
            response.setContentType("application/octet-stream");//text/plain
            response.setHeader("Content-Disposition", "inline;filename=" + new String(fileRealname.getBytes("UTF-8"), "ISO8859-1"));

            br = new BufferedInputStream(new FileInputStream(file));

            out = response.getOutputStream();
            byte[] b = new byte[1024];
            int len = 0;

            while ((len = br.read(b)) != -1) {
                out.write(b, 0, len);
            }
            //TODO 
            out.flush();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                }
                if(br != null){
                    br.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

1个回答

用户点击取消,socket 应该就不能往外写了
此时在catch 块里已经处理了,报错不是正常的吗

qq_28537803
qq_28537803 是的,我意思是怎么让用户取消时web后台不报错啊?不知该怎么解决。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐