zoukankan      html  css  js  c++  java
  • Java之Servlet文件下载20190228

    jsp页面:

    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="DownloadServlet?urlStr=index.jsp">下载index.jsp</a>
    <a href="DownloadServlet?urlStr=photo/1.jpg">下载1.jpg</a>
    <a href="DownloadServlet?urlStr=test.jsp">下载test.jsp</a>
    </body>
    </html>

    Servlet:

    package org.jimmy.testwebproject2019012602.servlet.download;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * @author Yang.Yuxin(Jimmy)
     * @date 2019年2月14日 下午5:39:47
     * @detail 下载Servlet
     */
    public class DownloadServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                request.setCharacterEncoding("utf-8");
                response.setCharacterEncoding("utf-8");
                String urlStr = request.getParameter("urlStr");
                String fileName = urlStr.substring(urlStr.lastIndexOf("/") + 1);
                response.setContentType("application/x-msdownload; charset=utf-8");
                response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
                //这个是使用项目的相对路径来获取文件的输入流
                InputStream is = request.getServletContext().getResourceAsStream(urlStr);
                /*
                //这个可以使用绝对路径来获取文件的输入流
                FileInputStream fis = new FileInputStream("D:\Document\201902\20190228\Test\1.jpg");
                */
                ServletOutputStream sos = response.getOutputStream();
                int fileLength = is.available();
                int cacheLength = 1024;
                if(fileLength < cacheLength) {
                    cacheLength = fileLength;
                }
                int len = cacheLength;
                byte[] bytes = new byte[cacheLength];
                while((len = is.read(bytes, 0, len)) != -1) {
                    sos.write(bytes);
                }
                sos.flush();
                sos.close();
                is.close();
            }catch(Exception e) {
                e.printStackTrace();
            }
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
        
    }
    2015年10月-2016年3月 总计:5个月.
    2016年11月-2017年6月 总计:7个月.
    2017年7月-2018年4月 总计:9个月.
    2018年5月-2018年5月 总计:1个月.
    2018年6月-2018年12月 总计:6个月.
    2019年1月-2019年12月 总计11个月.
    2020年2月-2021年2月 总计13个月.
    所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
    本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
    英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
  • 相关阅读:
    node generator 模仿co
    node-webkit 屏幕截图功能
    linux命令, cut,sort,wc,uniq,tee 说明
    linux命令,vim,vi 说明
    linux命令,tar,configure,make,make install,su 说明
    java高级工程师学习方向
    oracle: Rownum原理
    Win7 环境weblogic用户名和密码忘记解决方法
    struts原理介绍,面试
    JSP、servlet--学习摘要
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/10449017.html
Copyright © 2011-2022 走看看