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

Posted 80后程序猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 文件下载功能 解决中文乱码的主要内容,如果未能解决你的问题,请参考以下文章

java数据库中文乱码怎么解决

Java AES解密 中文乱码

java文件下载以及中文乱码解决

form表单提交中文乱码(前台中文到JAVA后台乱码)问题及解决

Java Mina Socket通信 中文乱码

jar在eclipse中乱码怎么解决