[问题] 动态取得图档时 参数编码问题

楼主: stw82 (Godhand)   2015-09-21 11:14:50
使用的框架是Spring MVC, 由于必须从非context path下的特定路径
取得图档显示在画面上, 写了一个如文末附的method
在页面上 img的src指向controller后
透过传入实际路径来取得图档并扔给ServletOutputStream
目前的状况是 档案名称只包含英文跟数字的话没有问题 但只要含有中文时
request.getParameter("path")取得的变量就会有乱码
有试过request.setCharacterEncoding("UTF-8");
(其实有先print过request.getCharacterEncoding(), 已经是UTF-8)
也试过把${imgPath}给encode后 后端controller用
URLDecoder.decode(request.getParameter("path"),"UTF-8")
但无论怎试 出来的依然还是乱码 不知道是否有人碰过类似的问题?
该如何去设定encoding才能取得正确的包含中文的档名路径?
<img src="./myController?method=getImage&path=${imgPath}"/>
@RequestMapping(params = "method=getImage")
public void getImage(HttpServletResponse response ,HttpServletRequest
request) throws IOException{
String mimeType = context.getMimeType(request.getParameter("path"));
response.setContentType((mimeType != null) ? mimeType :
"application/octet-stream");
ServletOutputStream out = response.getOutputStream();
InputStream in = new FileInputStream(request.getParameter("path"));
IOUtils.copy(in, out);
in.close();
out.close();
}
作者: kojilin (阿~~小达达)   2015-09-21 14:00:00
我没记错http get 不会走 setCharacterEncoding.tomcat 就有 URIEncoding 的设定。
楼主: stw82 (Godhand)   2015-09-21 15:14:00
下午试了一下 要encode两次以后 在后端用URLDecoder才能取得正确的值
作者: Dnight (暗夜)   2015-09-21 18:25:00
你的中文档名编码确定不是big5是UTF-8吗?
作者: PttTime   2015-09-21 21:36:00
server.xml Connector useBodyEncodingForURI="true"
作者: jay80915 (一种感觉)   2015-09-21 23:15:00
用get取得参数需要在转一次,String newPath = new String(path.getBytes("ISO-8859-1"), "UTF-8");

Links booklink

Contact Us: admin [ a t ] ucptt.com