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

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

          Posted on 2005-12-05 10:47 Terry的Blog 閱讀(624) 評論(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中得到了解決.

          主站蜘蛛池模板: 南雄市| 和田市| 景洪市| 内黄县| 河池市| 健康| 赤城县| 德江县| 郸城县| 浦东新区| 綦江县| 安丘市| 四平市| 中超| 永仁县| 洪泽县| 喀什市| 张家港市| 河西区| 荆州市| 汾西县| 渑池县| 呼和浩特市| 城固县| 白沙| 金坛市| 杨浦区| 梁山县| 广汉市| 彭阳县| 奇台县| 凤山市| 吉木乃县| 四子王旗| 福建省| 磐安县| 镇赉县| 哈巴河县| 台中县| 乐业县| 饶河县|