本文实例为大家分享了Servlet实现文件下载的具体代码,供大家参考,具体内容如下
把文件目录直接暴露给用户是很不安全的。所以要用Servlet来做,而且这样做,文件的存储方式就更丰富了,可以是从文件系统上取来的,也可以是数据库中经过计算生成的,或者从其它什么稀奇古怪的地方取来的。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
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();
}
}
}
|
web.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< servlet >
< servlet-name >downloadservlet-name>
< servlet-class >org.mstar.servlet.DownloadServletservlet-class>
< init-param >
< param-name >fileRootparam-name>
< param-value >d:/tempparam-value>
init-param>
< init-param >
< param-name >contentTypeparam-name>
< param-value >application/x-msdownloadparam-value>
init-param>
< init-param >
< param-name >encparam-name>
< param-value >utf-8param-value>
init-param>
servlet>
< servlet-mapping >
< servlet-name >downloadservlet-name>
< url-pattern >/downurl-pattern>
servlet-mapping>
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/mingzi/archive/2009/03/09/1406810.html
相关文章
猜你喜欢
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
如何在亚马逊云主机上部署WordPress等主流CMS系统?
2025-05-27 53 -
Windows 11自带杀毒软件太灵敏了,可以这样暂时关闭或永久禁用它
2025-05-27 90 -
2025-05-25 12
-
2025-06-04 66
-
2025-05-29 48
热门评论