首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp文件下载有关问题:Connection reset by peer: socket write error

2012-03-13 
jsp文件下载问题:Connection reset by peer: socket write error我用以下方法实现资源下载功能:首先是一个

jsp文件下载问题:Connection reset by peer: socket write error
我用以下方法实现资源下载功能:

首先是一个客户端JSP页面,显示可下载的资源列表,当用户点击某个资源是,传递一个唯一的资源编号到Action,在数据库中读取该资源的url,然后下载次数+1,然后把url放到一个request里面,再跳到一个JSP页面:

HTML code
<%@ page language="java" pageEncoding="GB18030"%><jsp:directive.page import="java.net.URLEncoder"/><%    request.setCharacterEncoding("GB18030");    String srcURL = String.valueOf(request.getAttribute("srcURL"));//资源路径       response.setContentType("application/x-download");       String url = "/upload/doc/"+srcURL;       String fileName = srcURL;       fileName = URLEncoder.encode(fileName, "UTF-8");       response.addHeader("Content-Disposition", "attachment;filename="               + fileName);       RequestDispatcher rd = application.getRequestDispatcher(url);       rd.forward(request, response);       response.flushBuffer();       out.clear();       out.clearBuffer();       out = pageContext.pushBody();%>


这时候,可以下载资源了,但是我发现点击某些资源,弹出保存框,并且取消保存的时候,控制台输出如下错误:

Java code
2009-8-26 22:06:58 org.apache.catalina.core.ApplicationDispatcher invoke严重: Servlet.service() for servlet jsp threw exceptionjava.net.SocketException: Connection reset by peer: socket write error    at java.net.SocketOutputStream.socketWrite0(Native Method)    at java.net.SocketOutputStream.socketWrite(Unknown Source)    at java.net.SocketOutputStream.write(Unknown Source)    at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:750)    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:432)    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:347)    at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:773)    at org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:118)    at org.apache.coyote.http11.InternalOutputBuffer.doWrite(InternalOutputBuffer.java:583)    at org.apache.coyote.Response.doWrite(Response.java:560)    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:361)    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:432)    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:347)    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:392)    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:381)    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)    at org.apache.catalina.servlets.DefaultServlet.copyRange(DefaultServlet.java:2083)    at org.apache.catalina.servlets.DefaultServlet.copy(DefaultServlet.java:1796)    at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:874)    at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:376)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)    at org.apache.jsp.mainViews.downloadAction_jsp._jspService(downloadAction_jsp.java:56)    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:679)    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:461)    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:399)    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)    at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)    at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)    at java.lang.Thread.run(Unknown Source)2009-8-26 22:06:58 org.apache.catalina.core.StandardWrapperValve invoke 



有谁对这个错误了解的吗??有解决方案吗??

[解决办法]
给你个下载的源码吧,很简单的.
http://download.csdn.net/source/970932
[解决办法]
报connection reset by peer的时候, 就是因为服务器端向客户端传送数据, 但是客户端已经断开请求.

这不影响使用. 
还有另外一个问题不知道你有没有遇到, 那就是当点击的时候如果你机器安装了迅雷的话, 而你点击了迅雷的取消, 这时, 文件实际被下载了两次.
[解决办法]
response.flushBuffer();
out.clear();
out.clearBuffer();
可能是这里的原因啊,你清空了两次缓存!
[解决办法]
用flush()
[解决办法]
探讨
报connection reset by peer的时候, 就是因为服务器端向客户端传送数据, 但是客户端已经断开请求.

这不影响使用.
还有另外一个问题不知道你有没有遇到, 那就是当点击的时候如果你机器安装了迅雷的话, 而你点击了迅雷的取消, 这时, 文件实际被下载了两次.

[解决办法]
public Hashtable<String, Object> saveWeekly(
Hashtable<String, Object> hsTmp, String basePath, TblUserR user,
MtweeklyForm mwf, HttpServletRequest request, String realPath) {
String msg = "保存成功";//
TBmpMtannex tbx = new TBmpMtannex();//
FormFile myfile = mwf.getMyfile();//
String filename = myfile.getFileName();//
int size = myfile.getFileSize();//
String type = myfile.getContentType();//
System.out.println(filename);//
System.out.println(size);//
System.out.println(type);//
FileOutputStream fos = null;
try {
// myfile.getInputStream();
byte[] date = myfile.getFileData();
// Java如果要访问服务器硬盘,一定要提供逻辑绝对路径(硬盘上的路径C:\..),应用的默认在tomcat的bin目录下;
// 怎么样将URL相对路径/files/转成硬盘上的绝对路径?用application在Action里
fos = new FileOutputStream(realPath + "/" + filename, true);//
System.out.print("????????????????????????????" + realPath);
fos.write(date);//
} catch (RuntimeException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();//
} catch (IOException e) {

e.printStackTrace();
}
}
tbx.setMFileName(filename);//
tbx.setMSpace(realPath);// 路径是否是绝对路径??????????
tbx.setMUploadDate(new Date());//
tbx.setMUploadUser(user.getMName());//
tbx.setMIsdel("0");//
tbx.setMFileInfo("大小:" + size + ";类型:" + type);//
try {
daoTool.saveOrUpdate(tbx);//

} catch (RuntimeException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("这里出错@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" + ex);
}
TBmpMtweekly tbmly = new TBmpMtweekly();//
String mfileId = tbx.getMFileId();// 附件ID
System.out.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
+ tbx.getMFileId());
// 先保存文件在保存主业务表数据 这样可以用附件ID 在下载时找到要下载的文件
String mreportype = request.getParameter("mreporttype") == null ? ""
: request.getParameter("mreporttype");// 报表类型
//TU2d tud=new TU2d();
String deptname = ((TU2d) daoTool.find(
"from TU2d as a where a.tblUserR = '" + user.getMUserid() + "'").get(0))//
.getTDept().getWDeptName();//
String mupplace = deptname;// 上传地市;
String mreporttime = request.getParameter("mreporttime") == null ? ""
: request.getParameter("mreporttime");// 报表时间
tbmly.setMAnswerPlace("全省各地市");
tbmly.setMFileId(mfileId);//
tbmly.setMFileName(tbx.getMFileName());//
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + tbx.getMFileName());


tbmly.setMIsdel("0");//
tbmly.setMReportTime(StringUtil.strToDate(mreporttime));//
tbmly.setMTemp1(mupplace);//
tbmly.setMReportType(mreportype);//
tbmly.setMUploadTime(tbx.getMUploadDate());//
tbmly.setMUploadUser(tbx.getMUploadUser());//
try {
daoTool.saveOrUpdate(tbmly);//
} catch (RuntimeException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("错误发生了哎!!¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥" + ex);
}
System.out.println("00000000000000000000000000000000000"+tbmly.getMReportTime());
String mttime=StringUtil.dateToStr(tbmly.getMReportTime(),"yyyy-MM-dd");//
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+mttime);
String hql="from TBmpMtdept as a where a.MTemp='"+mttime+"'and a.MTemp1='"+tbmly.getMReportType()+"'";//
List list001=daoTool.find(hql);
//在这里加个上传时间的判断就是没有及时回复和没回复的就区分出来了;时间大于要求的时间就不保存了;
TBmpMtdept tbdept=new TBmpMtdept();//
if(list001.size()>0){
tbdept=(TBmpMtdept)list001.get(0);
//对于删除后重复上传的的地市编号 要判断是否存在了 要是存在 就不需要再加入这个地市编号了;
tbdept.setMDeptNumber(tbdept.getMDeptNumber()+" "+tbmly.getMFileName().substring(8, 11));//
try{
daoTool.saveOrUpdate(tbdept);
}catch (RuntimeException ex) {
ex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
System.out.println("倒霉这里出错了哎!!1》》》》》》》》》》》》》》》》》》》》》》" + ex);
}
}else{
tbdept.setMDeptNumber(tbmly.getMFileName().substring(8, 11));//地市号码集
tbdept.setMIsdel("0");
tbdept.setMTemp(StringUtil.dateToStr(tbmly.getMReportTime(),"yyyy-MM-dd"));//周报时间
tbdept.setMTemp1(tbmly.getMReportType());//报表类型
tbdept.setMTemp2(StringUtil.dateToStr(tbmly.getMUploadTime(),"yyyy-MM-dd hh:mm:ss"));//上传时间

try{
daoTool.saveOrUpdate(tbdept);
}catch (RuntimeException ex) {
ex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
System.out.println("倒霉这里出错了哎!!2<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<," + ex);
}
}
我的上传

热点排行