一个文件下载的Servlet
2015-12-24 17:39
253 查看
转载自:http://www.cnblogs.com/mingzi/archive/2009/03/09/1406810.html
把文件目录直接暴露给用户是很不安全的。所以要用Servlet来做,而且这样做,文件的存储方式就更丰富了,可以是从文件系统上取来的,也可以是数据库中经过计算生成的,或者从其它什么稀奇古怪的地方取来的。
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
<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>
把文件目录直接暴露给用户是很不安全的。所以要用Servlet来做,而且这样做,文件的存储方式就更丰富了,可以是从文件系统上取来的,也可以是数据库中经过计算生成的,或者从其它什么稀奇古怪的地方取来的。
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
<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>
相关文章推荐
- ListView
- C语言开发总结(二十六)
- android Error:Execution failed for task 'xxxxx:mergeDebugResources'. > Crunching Cruncher ic
- JS滑动下划线导航菜单实现原理
- java swing 打开浏览器
- ./src/video/x11/SDL_x11sym.h:168:17: 错误:与‘_XData32’类型冲突
- 结构型:设计模式之装饰模式(十一)
- Hibernate的持久化状态
- [leetcode] 110. Balanced Binary Tree 解题报告
- Java 8十个lambda表达式案例
- OC学习篇之-----KVC
- Swift主题色顶级解决方案
- [css]我要用css画幅画(七) - 哆啦A梦
- Python的数据类型
- android zxing自定义界面,点击按钮开关闪光灯
- MFC与C#连接MYSQL乱码问题
- Servlet 实现下载功能
- js封装时间转换工具,实现日期之前的转换
- 技术人员如何去面试?
- 接口学习