困扰我很久的jsp下载异常,望哪位大侠救我!在线狂等!
源码如下:
<%@ page contentType="text/html;charset=gbk"%>
<%@ page language="java" import="java.io.*,java.net.*"pageEncoding="gbk"%>
<%@ page import="com.kehong.model.dao.*,com.kehong.model.bean.*"%>
<html>
<head>
<title>test</title>
</head>
<body>
<%
try{
response.reset();
String mid = request.getParameter("mid");
FileBean fb = FileDAO.getInstance().getProById1(Integer.parseInt(mid));
String path = fb.getPic().trim();
int k = path.lastIndexOf("/");
String name = path.substring(k + 1, path.length());
response.setContentType("text/html");
javax.servlet.ServletOutputStream ou = response.getOutputStream();
System.out.println("DownloadFile filepath:" + path);
//System.out.println("DownloadFile filename:"+ request.getRealPath("") + name);
java.io.File file = new java.io.File(request.getRealPath("") + path);
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + " 文件不存在!");
return;
}
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file);
if (name != null && name.length() > 0) {
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment; filename="+ new String(name.getBytes("gb2312"),"iso8859-1") + "");
if (fileInputStream != null) {
int filelen = fileInputStream.available();
byte a[] = new byte[filelen];
fileInputStream.read(a);
ou.write(a);
}
}
ou.close();
ou=null;
out.clear();
out = pageContext.pushBody();
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
Client
AbortException:
java.net.SocketException: Connection reset by peer: socket write error at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:354)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:381)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:83)
at org.apache.jsp.download1_jsp._jspService(download1_jsp.java:92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.kehong.filter.Encoding.doFilter(Encoding.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValv