大家好,又见面了,我是你们的朋友全栈君。
以前没写过文件下载的程序,现在在servlet中试了试,有些问题请教大神。代码如下:publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException…
以前没写过文件下载的程序,现在在servlet中试了试,有些问题请教大神。代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
response.setCharacterEncoding(“utf-8”);
// String path = request.getParameter(“path”);
String path = “F:\OKDownLoad\854546_e472e815bf086a04a78173d95c90b760_1369553818.gif”;
//读到流中
InputStream ins = new FileInputStream(path);
//设置输出的格式
response.reset();
response.setContentType(“bin”);
response.addHeader(“Content-Disposition”, “attachment; filename=”下载测试””);
//循环取出流中的数据
byte[] b = new byte[100];
int len = ins.read(b);
ins.close();
try {
while(len > 0){
response.getOutputStream().write(b, 0, len);
ins.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
这样下载的时候,会弹出下载狂,但是就一直卡在0%,下载不动,一点击取消,就报出ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error异常,各位大神指教指教啊。
展开
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/157181.html原文链接:https://javaforall.cn