Spring mvc 下载文件 取消报异常

SmileTower 发布于 2015/09/07 16:53
阅读 1K+
收藏 1
public ResponseEntity<byte[]> downLoadBook(HttpServletRequest request) {
        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpState = HttpStatus.NOT_FOUND;
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//Content-Type
        byte[] bytes = null;
        try {
        String fileName = URLEncoder.encode("1.zip", "UTF-8");
             headers.add("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + fileName);
             File f = new File("D:\1.zip");
             if (f.exists() && f.isFile()) {
                 bytes = FileUtils.readFileToByteArray(f);
                 headers.setContentLength(bytes.length);
                 httpState = HttpStatus.OK;
             }
} catch (IOException e) {
e.printStackTrace();
}
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(bytes, headers, httpState);

        return entity;

 }

Spring mvc 下载文件 取消报异常

org.apache.catalina.connector.ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error

怎么处理?

用传统的response的方式下载,取消时不会报异常;求大神解答

加载中
0
男孩boy
男孩boy
好像我也碰到过,下载后要关闭或者清空资源,期待答案
返回顶部
顶部