下面是我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();
}
}
}