免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 889 | 回复: 0
打印 上一主题 下一主题

一个文件下载的Servlet [复制链接]

论坛徽章:
10
数据库技术版块每日发帖之星
日期:2015-06-14 22:20:00数据库技术版块每日发帖之星
日期:2016-03-10 06:20:00数据库技术版块每日发帖之星
日期:2015-12-01 06:20:00IT运维版块每日发帖之星
日期:2015-11-09 06:20:00IT运维版块每日发帖之星
日期:2015-11-02 06:20:00IT运维版块每日发帖之星
日期:2015-07-13 22:59:28IT运维版块每日发帖之星
日期:2015-06-23 22:20:00程序设计版块每日发帖之星
日期:2015-06-21 22:20:00每日论坛发贴之星
日期:2015-06-14 22:20:00IT运维版块每日发帖之星
日期:2016-08-02 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-10 15:53 |只看该作者 |倒序浏览

一个文件下载的Servlet关键词
文件下载
   
Servlet
                                          
把文件目录直接暴露给用户是很不安全的。所以要用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) {
                /*创建输入浿/SPAN>*/
                InputStream inStream = new FileInputStream(file);
                byte[] buf = new byte[4096];
                /*创建输出浿/SPAN>*/
                ServletOutputStream servletOS = response.getOutputStream();
                int readLength;
                while (((readLength = inStream.read(buf)) != -1)) {
                    servletOS.write(buf, 0, readLength);
                }
                inStream.close();
                servletOS.flush();
                servletOS.close();
            }
        }
    }/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>
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>
        

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/11905/showart_97790.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP