您的位置 首页 技术

java下载乱码解决方法

下载文件乱码解决代码: String userAgent = request.getHeader("User-Agent"); String formFileN…

下载文件乱码解决代码:

  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[]解析成字符串。

更多java知识请关注java基础教程栏目。

以上就是java下载乱码解决方法的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/25413.html

为您推荐

返回顶部