public
class
DownloadServlet
extends
HttpServlet {
private
String contentType
=
"
application/x-msdownload
"
;
private
String enc
=
"
utf-8
"
;
private
String fileRoot
=
""
;
/**
* 初始化contentType,enc,fileRoot
*/
public
void
init(ServletConfig config)
throws
ServletException {
String tempStr
=
config.getInitParameter(
"
contentType
"
);
if
(tempStr
!=
null
&&
!
tempStr.equals(
""
)) {
contentType
=
tempStr;
}
tempStr
=
config.getInitParameter(
"
enc
"
);
if
(tempStr
!=
null
&&
!
tempStr.equals(
""
)) {
enc
=
tempStr;
}
tempStr
=
config.getInitParameter(
"
fileRoot
"
);
if
(tempStr
!=
null
&&
!
tempStr.equals(
""
)) {
fileRoot
=
tempStr;
}
}
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
String filepath
=
request.getParameter(
"
filepath
"
);
String fullFilePath
=
fileRoot
+
filepath;
/*
读取文件
*/
File file
=
new
File(fullFilePath);
/*
如果文件存在
*/
if
(file.exists()) {
String filename
=
URLEncoder.encode(file.getName(), enc);
response.reset();
response.setContentType(contentType);
response.addHeader(
"
Content-Disposition
"
,
"
attachment; filename=\
""
+ filename +
"
\
""
);
int
fileLength
=
(
int
) file.length();
response.setContentLength(fileLength);
/*
如果文件长度大于0
*/
if
(fileLength
!=
0
) {
/*
创建输入流
*/
InputStream inStream
=
new
FileInputStream(file);
byte
[] buf
=
new
byte
[
4096
];
/*
创建输出流
*/
ServletOutputStream servletOS
=
response.getOutputStream();
int
readLength;
while
(((readLength
=
inStream.read(buf))
!=
-
1
)) {
servletOS.write(buf,
0
, readLength);
}
inStream.close();
servletOS.flush();
servletOS.close();
}
}
}
=======================================================================
public class Download extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
//TODO Method stub generated by Lomboz
}
public void destroy() {
super.destroy();
//TODO Method stub generated by Lomboz
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String root = getServletContext().getRealPath("/");
String path = request.getParameter("path");
String name = request.getParameter("name");
response.setContentType("text/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=\"" + name + "\"");
try
{
java.io.OutputStream os = response.getOutputStream();
java.io.FileInputStream fis = new java.io.FileInputStream(root + path + name);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 )
{
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
========================================================================
分享到:
相关推荐
豆丁网免费下载器(冰点)无需登录也无需积分,只需要将需要下载的文档的网页地址复制在软件的录入框中就可以自由下载,并最终生成pdf文件。对部分pdf文件能够提取文字生成txt。 豆丁网免费下载器还支持自由下载百度、...
用ajax实现的java文件打包下载。可以输入扩展名选择文件下载。
在线下载的方法(付费一2般不可能全部下载!
由于在学习numpy数据分析的时候没有csv类型文件,又懒得自己一个个敲字,便想来csdn里随便下载一个。然而......居然要钱啊!!!!!!!我没钱,所以自己写了个,我不要你们的钱,麻烦各位拿资源的时候吱一声,这是...
visual studio 2010的帮助文档很难下载,一方面文件很多很大,全部文档超过1GB,使用微软自带的 或者辅助工具,由于没有发挥多线程和断点续传的特点,下载速度很慢,容易失败。 另一方面迅雷等下载工具,可以实现...
ASP.NET中提供可以在页面下载存储在后台的文件,很好用哦
用Visuall C++实现远程文件下载,这只是word文档。。。
将豆丁文档转化为pdf 免除豆丁收费要求
utf-8文档下载,有需要的可以下载一下,
Java文件下载的几种方式,是很方便的,找了半天找到了,共享一下
豆丁网文件一般只能看,不能下,本工具可把豆丁网文件下载到本地,并保存成pdf文件
库函数stdio.h文件下载~………………
1、在本站下载解压,得到小叶文库下载器软件包; 2、首先前往文库网站找到需要下载的文库 3、将其文库地址复制粘贴到软件中,进行解析下载;...4、之后下载好的文件可在本自录下的Download文件夹进行查看。
redis.conf Redis配置文件 下载 redis.conf 配置详解 Redis配置文件redis.conf 详解1.基本配置内存单位的表示# 1k => 1000 bytes# ...Redis 的详细介绍Redis 的下载地址
Java实现文件下载功能,以WORD文档方式展现给用户看。
豆丁网文档下载器 第一次运行请先运行bat文件,当文件处理时请保证您的当前磁盘有足够的空间
Visual Studio 2012 官方离线文档 下载地址
python
sharepoint 2010 实现文档下载次数
文件下载的jsf程序...................(用poi导出excel文档)