您的位置 首页 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[]解析成字符串。

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

文章来源:智云一二三科技

文章标题:java下载乱码解决方法

文章地址:https://www.zhihuclub.com/20409.shtml

关于作者: 智云科技

热门文章

网站地图