日期:2014-05-20  浏览次数:20789 次

哥哥们,求教!文件下载,用户取消文件接受,报异常ClientAbortException
ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
查了很多资料貌似有没好的解决方法:
引用:“此错误会在你输出文件流后客户端强制中断接收时产生。正常情况下不会产生。由你说的“jsp会执行很多次” 可以判断出必然出现这个异常。因为只有一次会正常被客户端接收。你只需要解决为什么你的这个页面会多次向客户端浏览器输出就可以了。 ”

怎么样才会不报这个异常呢?

代码:
public ActionForward fileDownLoad(ActionMapping mapping,
  HttpServletRequest request, HttpServletResponse response)
  throws ServletException {
  String fileName = null;// 名称
  
  String realpath = "D:/crmSite/cdoc/";
  
  realpath = "D:/crmSite/cdoc/"
  + adform.getAdMat().substring(1);
  
  realpath = StrUtils.replace(realpath, "//", "/");
  
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  OutputStream fos = null;
  InputStream fis = null;
  fileName = realpath.substring(realpath.lastIndexOf("/") + 1, realpath.length());
  //System.out.println(realpath);
  try {
  response.setContentType(this.getContentType(fileName));
  response.setHeader("Content-disposition", "attachment;filename="
  + fileName);
  fis = new FileInputStream(realpath);
  bis = new BufferedInputStream(fis);
  fos = response.getOutputStream();
  bos = new BufferedOutputStream(fos);

  int bytesRead = 0;
  byte[] buffer = new byte[5 * 1024];
  while ((bytesRead = bis.read(buffer)) != -1) {
  bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
  }
  bos.close();
  bis.close();
  fos.close();
  fis.close();

  } catch (IOException e) {
  response.reset();
  e.printStackTrace();
  } finally {
  try {
  if (fos != null) {
  fos.close();
  }
  if (bos != null) {
  bos.close();
  }
  if (fis != null) {
  fis.close();
  }
  if (bis != null) {
  bis.close();
  }
  } catch (IOException e) {

  System.err.print(e);
  }
  }
  return null;
 }


------解决方案--------------------
是不是关闭文件的顺序错了?一般都是先关in再关out。。



bos.close();
bis.close();
fos.close();
fis.close();
------解决方案--------------------
以前遇到过这个问题,这个异常好像是点击取消就一定会有的,但是异常只会在后台显示不会影响程序的,可以不用管
------解决方案--------------------
这个如果这么做的话,是肯定出异常的。因为相当于你穿数据流给客户端是一个响应。而正常的响应依然会存在。这就是两个了。
如果想不要出异常。你可以建一个servlet。这个servlet只负责传送文件。这样就不会看到异常信息了。至于具体怎么做,你可以在网上再查查。我这暂时没有例子。
------解决方案--------------------
下列情况下可能产生以上问题:
1.用户刷新请求,即重复提交
2.用户在下载窗口取消下载
3.网络异常
第一种情况可以通过程序控制,后两种情况可以忽略