java文件下载断点续传
2016-06-23 15:53
441 查看
public static void outputFile(HttpServletResponse response,
HttpServletRequest request,
String fileName,
InputStream is,
long fileSize) throws FileNotFoundException,UnsupportedEncodingException, IOException {
response.setContentType("application/octet-stream");
javax.servlet.ServletOutputStream outputStream = response.getOutputStream();
String range = request.getHeader("RANGE");
long p = 0;
// tell the client to allow accept-ranges
response.reset();
response.setHeader("Accept-Ranges", "bytes");
// client requests a file block download start byte
if (range != null) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
p = Long.parseLong(range.replaceAll("bytes=", "").replaceAll("-",""));
}
// response.setHeader("Content-Length", new Long(fileSize).toString());
response.setHeader("Content-Length", new Long(fileSize - p).toString());
response.setHeader("Content-Disposition", "attachment; filename=\""
+ java.net.URLEncoder.encode(fileName, "UTF-8") + "\"");
if (p != 0) {
// 断点开始
// 响应的格式是:
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
String contentRange = new StringBuffer("bytes ")
.append(new Long(p).toString()).append("-")
.append(new Long(fileSize - 1).toString()).append("/")
.append(new Long(fileSize).toString()).toString();
response.setHeader("Content-Range", contentRange);
// pointer move to seek
is.skip(p);
}
try {
if (is != null) {
byte[] buf = new byte[1024];
int num = 0;
while ((num = is.read(buf)) != -1) {
outputStream.write(buf, 0, num);
outputStream.flush();
}
}
} catch (IOException e) {
logger.error(e.toString());
} finally {
if (null != is) {
is.close();
is = null;
}
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
}
}
HttpServletRequest request,
String fileName,
InputStream is,
long fileSize) throws FileNotFoundException,UnsupportedEncodingException, IOException {
response.setContentType("application/octet-stream");
javax.servlet.ServletOutputStream outputStream = response.getOutputStream();
String range = request.getHeader("RANGE");
long p = 0;
// tell the client to allow accept-ranges
response.reset();
response.setHeader("Accept-Ranges", "bytes");
// client requests a file block download start byte
if (range != null) {
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
p = Long.parseLong(range.replaceAll("bytes=", "").replaceAll("-",""));
}
// response.setHeader("Content-Length", new Long(fileSize).toString());
response.setHeader("Content-Length", new Long(fileSize - p).toString());
response.setHeader("Content-Disposition", "attachment; filename=\""
+ java.net.URLEncoder.encode(fileName, "UTF-8") + "\"");
if (p != 0) {
// 断点开始
// 响应的格式是:
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
String contentRange = new StringBuffer("bytes ")
.append(new Long(p).toString()).append("-")
.append(new Long(fileSize - 1).toString()).append("/")
.append(new Long(fileSize).toString()).toString();
response.setHeader("Content-Range", contentRange);
// pointer move to seek
is.skip(p);
}
try {
if (is != null) {
byte[] buf = new byte[1024];
int num = 0;
while ((num = is.read(buf)) != -1) {
outputStream.write(buf, 0, num);
outputStream.flush();
}
}
} catch (IOException e) {
logger.error(e.toString());
} finally {
if (null != is) {
is.close();
is = null;
}
if (null != outputStream) {
outputStream.close();
outputStream = null;
}
}
}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- Scrapy的架构介绍
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序