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);
    }

}

 

转载于:https://www.cnblogs.com/wwcherish/p/8182895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值