jsp乱码问题,大家经常碰到,我在做多文件上传的过程中,基本碰到了所有的情况,解决也是曲折的,经过摸索,现在总结如下:
1、所有页面都用统一的编码UTF-8(或GB2312或GBK)。
2、写过滤器,设置request.setCharacterEncoding(“UTF-8”)。
3、javascript脚本里用传参数要先encodeURI(str)编码。
4、适当的时候,在jsp里用java.net.URLDecoder.decode(request.getParameter(“str”),”UTF-8″)解码。
5、最容易忽略的是在jsp页面使用”…..”:value=” <%=paraFiles%>”,一定要有引号。
6、文件下载的时候,response.setHeader的文件名要转换编码,读取文件的路径不要转换编码。
String fileName=request.getParameter(“filename”);
String contextPath=request.getSession().getServletContext().getRealPath(“”);
String filePath=contextPath+”//”+InitParameter.getUploadFile_Path()+”//”;
File file=new File(filePath+fileName);
String fileName1=toUtf8String(fileName);
response.setContentType(“application/octet-stream”);
response.setContentType(“application/OCTET-STREAM;charset=UTF-8”);
response.setHeader(“Content-Disposition”, “attachment;filename=”+fileName1);
例子源码:http://download.csdn.net/source/965319