您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java文件名中文乱码解决方法

2024/3/19 18:04:46发布22次查看
java文件下载时乱码有两种情况:(推荐:java视频教程)
1,下载时中文文件名乱码
2,下载时因为路径中包含中文文件名乱码,提示找不到文件
解决方法见下面部分代码
response.setcontenttype("multipart/form-data"); string useragent = request.getheader("user-agent"); string orafilename = meetingfile.getfilename(); string formfilename=orafilename; // 针对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("application/vnd.ms-excel;charset=utf-8"); response.setcharacterencoding("utf-8"); servletoutputstream out; // 通过文件路径获得file对象 file file = null; if (meetingfile != null) { file = new file(path + "upload/"+orafilename); }
(1)如果第一种乱码类型,下载页面遇到以下的中文乱码问题
用以下代码解决
(2)如果下载遇到第二种乱码问题,如图:
用以下代码解决:即首先确保tomcat ,eclipse等为utf-8编码
然后java中这样与第一种对文件名编码分开,使它们分别编码,互不影响。
更多java知识请关注java基础教程栏目。
以上就是java文件名中文乱码解决方法的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product