package com.archermind.util;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownloader {
public String download(String filePath, HttpServletResponse response,
HttpServletRequest request) {
try {
// 你可以使用你服务器上的文件及其路径
// RandomAccessFile 方式
java.io.RandomAccessFile raf = new java.io.RandomAccessFile(
filePath, "r");
response.reset();
response.setHeader("Server", "ad@Interdrp.com");
// 告诉客户端允许断点续传多线程连接下载
response.setHeader("Accept-Ranges", "bytes");
long p = 0;
long l = 0;
l = raf.length(); // RandomAccessFile 方式
// 如果是第一次下,还没有断点续传,状态是默认的 200,无需显式设置
if (request.getHeader("Range") != null) // 客户端请求的下载的文件块的开始字节
{
// 如果是下载文件的范围而不是全部,向客户端声明支持并开始文件块下载
// 要设置状态
// 响应的格式是:
// HTTP/1.1 206 Partial Content
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);// 206
// 从请求中得到开始的字节
// 请求的格式是:
// Range: bytes=[文件块的开始字节]-
p = Long.parseLong(request.getHeader("Range")
.replaceAll("bytes=", "").replaceAll("-", ""));
}
// 下载的文件(或块)长度
// 响应的格式是:
// Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
response.setHeader("Content-Length", new Long(l - p).toString());
if (p != 0) {
// 不是从最开始下载,
// 响应的格式是:
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
response.setHeader("Content-Range",
"bytes " + new Long(p).toString() + "-"
+ new Long(l - 1).toString() + "/"
+ new Long(l).toString());
}
// response.setHeader("Connection", "Close"); //如果有此句话不能用 IE 直接下载
// 使客户端直接下载,
// 响应的格式是:
// Content-Type: application/octet-stream
response.setContentType("application/octet-stream");
// 为客户端下载指定默认的下载文件名称
// 响应的格式是:
// Content-Disposition: attachment;filename="[文件名]"
response.setHeader("Content-Disposition", "attachment;filename=\""
+ filePath.substring(filePath.lastIndexOf("\\") + 1) + "\""); // RandomAccessFile
raf.seek(p);
// fis.skip(p);
byte[] b = new byte[1024];
int i;
while ((i = raf.read(b)) != -1) // RandomAccessFile 方式
{
response.getOutputStream().write(b, 0, i);
}
raf.close();
} catch (IOException e) {
e.printStackTrace();
return "error to download file";
}
return null;
}
}
红色部分代码循环抛异常了(手机uc浏览器8.3)
请帮我分析下错误原因
谢谢
ClientAbortException: java.net.SocketException: Software caused connection abort: socket write error
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
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:381)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:89)
at com.archermind.util.FileDownloader.download(FileDownloader.java:85)
at com.archermind.action.FileDownloadAction.frontDownload(FileDownloadAction.java:290)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at