星空网 > 软件开发 > Java

java web上传下载乱码问题解决方法

文件下载中文乱码,因为http请求url和header要求只能通过ascii码,对于其他字符则不行,需要转码。而不同浏览器的处理方式右不一样。

解决方法一:

	/**	 * 乱码解决	 * @throws UnsupportedEncodingException 	 * */	private static String toUtf8BytesString(String fileName, HttpServletRequest req) throws UnsupportedEncodingException {		//return new String(fileName.getBytes("GBK"), "ISO8859-1");		if(req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){			return URLEncoder.encode(fileName, "UTF-8");		}else{			return new String(fileName.getBytes("UTF-8"), "ISO8859-1");		}	}

 

解决方法二:

  这种方法在 windows 中文系统新版firefox、ie、chrome下下载不会乱码,不知道其他系统默认字符集非gbk的会不会有问题

new String(fileName.getBytes("gbk"), "ISO8859-1")

 

 

 

 参考资料:

http://www.ruanyifeng.com/blog/2010/02/url_encoding.html

 

  




原标题:java web上传下载乱码问题解决方法

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流