前言
问题展示
解决
前言
在这里我们演示了文件下载文件下载
问题展示
但是文件名有中文时会出现问题
文件,我们给 图片取了个中文名,美女
Servlet层代码
package com.lingaolu.download;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
/**
* @author 林高禄
* @create 2020-07-14-9:11
*/
@WebServlet("/dowmloadFile1")
public class DowmloadFile1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取文件名
String fileName = request.getParameter("fileName");
// 获取文件的MIME类型
ServletContext servletContext = this.getServletContext();
String mimeType = servletContext.getMimeType(fileName);
// 获取文件的真实路径
String realPath = servletContext.getRealPath("/file/" + fileName);
// 将文件读入内存
FileInputStream fis = new FileInputStream(realPath);
// 设置相应头类型
response.setHeader("content-type",mimeType);
response.setHeader("content-disposition","attachment;filename="+fileName);
// 将文件写出浏览器
ServletOutputStream outputStream = response.getOutputStream();
int len;
byte[] by = new byte[1024*8];
while((len = fis.read(by)) != -1){
outputStream.write(by,0,len);
}
fis.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
启动谷歌浏览器访问,单击图片,中文名变成了_
火狐浏览器访问,单击图片,中文名没有了
IE浏览器访问,单击图片,直接报错了,是因为IE浏览器版本太低了,不需要管
解决
因为每个浏览器出现的问题不一样,这是因为不同的浏览器的编码方式不一样,这个就需要我们针对不同的浏览器判断进行设置不同的编码方式
添加一个工具类DownLoadUtils,根据不同的浏览器进行不同的编码处理
package com.lingaolu.utils;
import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class DownLoadUtils {
public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// 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;
}
}
Servlet层代码
package com.lingaolu.download;
import com.lingaolu.utils.DownLoadUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
/**
* @author 林高禄
* @create 2020-07-14-9:11
*/
@WebServlet("/dowmloadFile1")
public class DowmloadFile1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取文件名
String fileName = request.getParameter("fileName");
// 获取文件的MIME类型
ServletContext servletContext = this.getServletContext();
String mimeType = servletContext.getMimeType(fileName);
// 获取文件的真实路径
String realPath = servletContext.getRealPath("/file/" + fileName);
// 将文件读入内存
FileInputStream fis = new FileInputStream(realPath);
// 获取user-agent请求头
String agent = request.getHeader("user-agent");
// 调用工具类,不同的浏览器进行不同的编码处理
fileName = DownLoadUtils.getFileName(agent, fileName);
// 设置相应头类型
response.setHeader("content-type", mimeType);
response.setHeader("content-disposition", "attachment;filename=" + fileName);
// 将文件写出浏览器
ServletOutputStream outputStream = response.getOutputStream();
int len;
byte[] by = new byte[1024 * 8];
while ((len = fis.read(by)) != -1) {
outputStream.write(by, 0, len);
}
fis.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
启动,谷歌浏览器访问,单击图片,中文名解决了
火狐浏览器访问,单击图片,中文名解决了