站长资讯网
最全最丰富的资讯网站

java下载乱码解决方法

java下载乱码解决方法

下载文件乱码解决代码:

  String userAgent = request.getHeader("User-Agent");       String formFileName = file.getFileName();                // 针对IE或者以IE为内核的浏览器:       if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {           formFileName = java.net.URLEncoder.encode(formFileName, "UTF-8");       } else {           // 非IE浏览器的处理:           formFileName = new String(formFileName.getBytes("UTF-8"), "ISO-8859-1");       }       response.setHeader("Content-disposition",String.format("attachment; filename="%s"", formFileName));       response.setContentType("multipart/form-data");        response.setCharacterEncoding("UTF-8");

URLEncoder.encode(String s, String enc) :使用指定的编码机制将字符串转换为 application/x-www-form-urlencoded 格式 。

new String(byte[],decode)方法使用指定的编码decode来将byte[]解析成字符串。

赞(0)
分享到: 更多 (0)

网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号