/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.topxp.struts;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
/**
* MyEclipse Struts
* Creation date: 10-01-2007
*
* XDoclet definition:
* @struts.action
* @struts.action-forward name="index" path="/resources/index.jsp"
*/
public class DownloadServerAction extends DispatchAction {
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
//读取资源,并提供下载
DownloadServerActionForm df = (DownloadServerActionForm)form;
File root = new File("/");
if(df.getName() != null && !df.getName().equals("")){
root = new File(df.getName());
}
request.setAttribute("path", root.getPath());
//提供下载
if(root.isFile()){
java.io.File f = root;
java.io.FileInputStream fis = new java.io.FileInputStream(f);
response.reset();
response.setHeader("Server", "playyuer@Microshaoft.com");
//告诉客户端允许断点续传多线程连接下载
//响应的格式是:
//Accept-Ranges: bytes
response.setHeader("Accept-Ranges", "bytes");
long p = 0;
long l = 0;
//l = raf.length();
l = f.length();
//如果是第一次下,还没有断点续传,状态是默认的 200,无需显式设置
//响应的格式是:
//HTTP/1.1 200 OK
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=\"" + s.substring(s.lastIndexOf("\\") + 1) + "\""); //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
String downloadname = URLEncoder.encode(f.getName());
response.setHeader("Content-Disposition", "attachment;filename=\"" + downloadname + "\"");
//raf.seek(p);
fis.skip(p);
byte[] b = new byte[1024];
int i;
//while ( (i = raf.read(b)) != -1 ) //经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
while ( (i = fis.read(b)) != -1 )
{
response.getOutputStream().write(b,0,i);
}
//raf.close();//经测试 RandomAccessFile 也可以实现,有兴趣可将注释去掉,并注释掉 FileInputStream 版本的语句
fis.close();
return null;
}else{
File[] files = root.listFiles();
if(root.getParentFile() != null){
String parentPath = root.getParentFile().getPath();
request.setAttribute("parent", parentPath);
}else{
request.setAttribute("parent", root.getPath());
}
request.setAttribute("files", files);
}
return mapping.findForward("index");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java断点续传功能代码
共49个文件
tld:20个
jar:11个
xml:3个
4星 · 超过85%的资源 需积分: 0 23 下载量 172 浏览量
2008-12-23
12:24:08
上传
评论
收藏 3.2MB RAR 举报
温馨提示
javaweb实现的断点续传功能代码 看看就知道了,是好东西
资源详情
资源评论
资源推荐
收起资源包目录
.rar (49个子文件)
断点续传功能代码
downloadserver.rar 1.6MB
downloadserver
.mystrutsdata 256B
src
org
topxp
struts
ApplicationResources.properties 91B
DownloadServerAction.java 4KB
DownloadServerActionForm.java 276B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
struts-nested.tld 70KB
validator-rules.xml 13KB
scriptfree.tld 2KB
c-1_0.tld 11KB
struts-bean.tld 9KB
fmt-1_0.tld 12KB
struts-html.tld 72KB
struts-logic.tld 14KB
struts-tiles.tld 8KB
fn.tld 7KB
web.xml 1KB
fmt.tld 19KB
x.tld 12KB
x-1_0-rt.tld 7KB
fmt-1_0-rt.tld 11KB
x-1_0.tld 7KB
.struts-config.mex 634B
sql.tld 8KB
classes
org
topxp
struts
ApplicationResources.properties 91B
DownloadServerAction.class 4KB
DownloadServerActionForm.class 600B
struts-config.xml 785B
sql-1_0.tld 6KB
sql-1_0-rt.tld 5KB
lib
commons-beanutils.jar 160KB
jakarta-oro.jar 62KB
standard.jar 384KB
commons-validator.jar 74KB
commons-fileupload.jar 19KB
commons-io-1.2.jar 64KB
jstl.jar 20KB
commons-logging.jar 33KB
struts.jar 474KB
antlr.jar 344KB
commons-digester.jar 147KB
c-1_0-rt.tld 11KB
permittedTaglibs.tld 1KB
c.tld 16KB
resources
index.jsp 611B
index.jsp 652B
.mymetadata 315B
.classpath 1KB
.myeclipse
共 49 条
- 1
逍遥书生
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 5G网络基础培训课件.zip
- 2024-spring-HIT-CS-大作业
- yolo目标检测项目实验
- downloadFile-1.hc
- C++课程设计:基于Qt的航班信息管理系统
- ADS7822UVerilog驱动,前面传的有点问题
- 基于python的高性能爬虫程序,使用了多线程+缓存+xpath实现的,这里以彼-岸图库为例,实现,仅用于学习交流
- 中分辨率成像光谱仪(MODIS)烧毁面积产品信息MODIS-C6-BA-User-Guide-1.2.pdf
- Screenshot_20240427_172613_com.huawei.browser.jpg
- 关于学习Python的相关资源网站链接及相关介绍.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3