• Java 文件下载功能 解决中文乱码


    Html部分

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.flv" class="downloadbtn">a.flv</a>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.jpg" class="downloadbtn">a.jpg</a>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.mp3" class="downloadbtn">a.mp3</a>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.mp4" class="downloadbtn">a.mp4</a>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.txt" class="downloadbtn">a.txt</a>
        <a href="http://localhost:8080/webdemojava/download1?filename=a.z1ip" class="downloadbtn">a.z1ip</a>
    </body>
    
    </html>

    Java代码

    package action;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLEncoder;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.jasper.tagplugins.jstl.core.Out;
    
    import sun.misc.BASE64Encoder;
    
    
    public class DownLoad1 extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
        public DownLoad1() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            
            
            String filename = request.getParameter("filename"); //获取发送过来的文件名
            filename = new String(filename.getBytes("ISO8859-1"),"UTF-8"); //解决传过来的文件名是中文.会变乱码的问题
            
            //获得请求头中的 User-Agent
            String agent = request.getHeader("User-Agent");
            //根据不同的客户的 进行不同的编码 .解决下载的文件 中文 没名字
            String filenameEncoder = "";
            if (agent.contains("MSIE")) {
                // IE浏览器
                filenameEncoder = URLEncoder.encode(filename, "utf-8");
                filenameEncoder = filenameEncoder.replace("+", " ");
            } else if (agent.contains("Firefox")) {
                // 火狐浏览器
                BASE64Encoder base64Encoder = new BASE64Encoder();
                filenameEncoder = "=?utf-8?B?"
                        + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
            } else {
                // 其它浏览器
                filenameEncoder = URLEncoder.encode(filename, "utf-8");                
            }
            
            
            String realPath = this.getServletContext().getRealPath("/download/"+filename); //相对路径变绝对路径
            InputStream in = null;
            try {
                in = new FileInputStream(realPath); //读到要下载的文件.并做检测文件是否存在
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            if (in == null) {
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter().append("没有找到该文件");
                return;
            }
            
            ServletOutputStream out = response.getOutputStream();
            response.setContentType(this.getServletContext().getMimeType(filename)); //设置返回的文件类型
            response.setHeader("Content-Disposition", "attachment;filename="+filenameEncoder); //告诉客户的文件以附件类型打开(下载)
    
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len=in.read(buffer))>0) {
                out.write(buffer, 0, len);
            }
            in.close();
            out.close();
            
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    Java面试题总结之JDBC 和Hibernate
    Java面试题总结之数据库与SQL语句
    Java面试题总结之OOA/D,UML,和XML
    Java面试题总结之数据结构、算法和计算机基础(刘小牛和丝音的爱情故事1)...
    文件路径的引用问题(配置文件路径vue.config.js)
    vue-cli2引入Bootstrap和jQuery
    ES6常用语法总结
    vue-cli4引入jquery和bootstrap
    vue-router的两种模式(hash和history)及区别
    本地存储localStorage的用法总结
  • 原文地址:https://www.cnblogs.com/wwcherish/p/8182895.html
Copyright © 2020-2023  润新知