解决浏览器下载文件时中文文件名乱码的问题

解决浏览器下载文件时中文文件名乱码的问题

很多时候我们需要在后台为前端提供文件下载的功能,但是当文件名中有中文时我们不能直接将文件名返回,需要对中文的文件名进行处理后再返回。

一、文件下载controller代码

import com.alibaba.fastjson.JSON;
import com.wang.mygateway.myapi.dto.BaseResponse;
import com.wang.mygateway.serviceportal.utils.DownLoadUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/files")
public class FileController {
    @RequestMapping("/download/{fileId}")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileId") String fileId) throws IOException {
        //根据fileId从数据库获取filePath,省略。。。。

        String filePath = "E:\\账号.txt";
        File file = new File(filePath);
        ServletOutputStream stream = null;

        try{
            stream = response.getOutputStream();
            response.reset();
            response.setContentType("multipart/form-data");
            response.setContentLength((int)file.length());
            String agent = request.getHeader("user-agent");
            String fileName = "";
            if (null != agent){
                fileName = DownLoadUtils.getFileName1(agent,file.getName());
            }else{
                fileName = new String(file.getName().getBytes("utf-8"),"ISO-8859-1");
            }
            response.addHeader("Content-Disposition","attachment;filename="+fileName);
            byte[] bytes = null;
            if (file.exists()&&file.isFile()&&file.canRead()){
                InputStream fileInputStream = new FileInputStream(file);
                long length = file.length();
                bytes = length > 0 ? IOUtils.toByteArray(fileInputStream,length) : IOUtils.toByteArray(fileInputStream);
            }
            stream.write(bytes);
            stream.flush();
        } catch (Exception e){
            this.writeResponse(response,404,"FILE000001","文件不存在");
            return;
        } finally {
            if (null != stream){
                stream.close();
            }
        }
    }
    private void writeResponse(HttpServletResponse response,int statusCode,String retCode,String retMsg) throws IOException {
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(retCode);
        baseResponse.setMsg(retMsg);
        response.setStatus(statusCode);
        response.setContentType("application/json");
        response.setHeader("Access-Control-Allow-Origin","*");
        response.getOutputStream().write(JSON.toJSONString(baseResponse,true).getBytes());
    }
}

二、文件名处理工具类

//BASE64Encoder类:jdk1.8及之前能用
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class DownLoadUtils {

    /**
     * 该方法要求JDK8及以下,可以针对不同的浏览器做不同的编码。
     * @param agent
     * @param filename
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
        if (agent.contains("MSIE")||agent.contains("Trident")) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐浏览器
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
        } else {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

    /**
     * 能满足大多数浏览器,并且不要求JDK版本,建议使用该方法。
     * @param agent
     * @param filename
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getFileName1(String agent, String filename) throws UnsupportedEncodingException {
        if (agent.contains("MSIE")||agent.contains("Trident")) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else {
            // 其它浏览器
            filename = new String(filename.getBytes("utf-8"),"ISO-8859-1");
        }
        return filename;
    }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值