liubinslt 发表于 2013-2-7 18:41:51

struts文件上传中文乱码的解决

用struts 1.3做文件上传(用的是struts自带的org.apache.struts.upload.FormFile实现文件的上传)的时候出现乱码的问题,即提交了包含file类型的input的页面(页面的编码为UTF-8)后,在action中取出参数时出了问题,具体的表现是页面提交的参数中参数值为中文的时候,在action中获得的参数值为乱码,如当上传的文件的文件名为中文时,在action中取得的文件名是乱码。
可用用下面方法解决这个问题:
在action中的execute最前面写入            
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception
{                           
request.setCharacterEncoding("UTF-8");
                   response.setContentType("text/html;chaset=UTF-8");                               FileForm fileForm=(FileForm) form;                  
String spCode= fileForm.getSpCode();
System.out.println("spCode"+spCode);
FormFile file=fileForm.getUpFile();//file是上传的文件名
System.out.println("文件名字是----"+file.getFileName());         
return null;
   }
页: [1]
查看完整版本: struts文件上传中文乱码的解决