java代码:
- public class DownloadServlet extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // codes..
- String name = "中文名 带空格 的测试文件.txt";
- String userAgent = request.getHeader("User-Agent");
- byte[] bytes = userAgent.contains("MSIE") ? name.getBytes() : name.getBytes("UTF-8"); // name.getBytes("UTF-8")处理safari的乱码问题
- name = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
- response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", name)); // 文件名外的双引号处理firefox的空格截断问题
- // codes..
- }
- }
共同学习一下,哈哈。。。