posts - 56, comments - 54, trackbacks - 0, articles - 4
   ::  ::  :: 联系 :: 聚合  :: 管理

Struts1.0.2中上传文件功能的Bug

Posted on 2005-12-05 10:47 Terry的Blog 阅读(608) 评论(0)  编辑  收藏 所属分类: java语言

Struts1.0.2中上传文件功能的Bug(日文文件名有时不能完整表示)
 
struts1.0.2中解析"multipart/form-data"型的request时没有根据request.getCharacterEncoding()的结果来解码.
当截取filename时就可能丢失一些字符.比如文件名为"ソウス.xls"

RequestUtil.java 
    public static void populate(Object bean, String prefix, String suffix,
                                HttpServletRequest request)
        throws ServletException {
   
        //initialize a MultipartRequestHandler
        MultipartRequestHandler multipart = null;
        String multipartClass = (String)
        request.getAttribute(Action.MULTIPART_KEY);
        request.removeAttribute(Action.MULTIPART_KEY);
        ......
        //在这里取处理MultipartRequest的类
        multipart = (MultipartRequestHandler) Class.forName(multipartClass).newInstance();
        ......
    }
    
    // 自定义一个DiskMultipartRequestHandlerX

    ActionServlet.java
    /**
     * The MultipartRequestHandler class name used for handling
     * multipart form requests.  This is the global default value,
     * the handler can also be set in individual mapping entries
     */
    protected String multipartClass = "org.apache.struts.upload.DiskMultipartRequestHandler";
   
    public class DefaultActionServlet extends ActionServlet {
   
        protected void process(HttpServletRequest request,
                               HttpServletResponse response) {
            try {
                String contentType = request.getContentType();
                String method = request.getMethod();
               
                //if this is a multipart request, wrap the HttpServletRequest object
                //with a MultipartRequestWrapper to keep the process sub-methods
                //from failing when checking for certain request parameters
                //for command tokens and cancel button detection
                if ((contentType != null) && (contentType.startsWith("multipart/form-data"))
                    && (method.equals("POST"))) {
                    //request.getAttribute(Action.MULTIPART_KEY);
                    // 设置处理MultipartRequest的类,也可以在struts-config.xml中设置。
                    request.setAttribute(Action.MULTIPART_KEY, "com.struts.upload.DiskMultipartRequestHandlerX");
                }
               

                request.setCharacterEncoding("Shift_JIS");
                super.process(request, response);
              
            } catch(Exception e) {
                log.error("encode error: ", e);
            }
        }
    }

这个问题在struts1.1中得到了解决.


只有注册用户登录后才能发表评论。


网站导航: