posts - 56, comments - 54, trackbacks - 0, articles - 4
             ::  ::  :: 聯(lián)系 :: 聚合  :: 管理

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

          Posted on 2005-12-05 10:47 Terry的Blog 閱讀(627) 評論(0)  編輯  收藏 所屬分類: java語言

          Struts1.0.2中上傳文件功能的Bug(日文文件名有時不能完整表示)
           
          struts1.0.2中解析"multipart/form-data"型的request時沒有根據(jù)request.getCharacterEncoding()的結(jié)果來解碼.
          當截取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中得到了解決.

          主站蜘蛛池模板: 正定县| 启东市| 始兴县| 泰兴市| 上杭县| 高碑店市| 安多县| 永仁县| 呼伦贝尔市| 庆阳市| 张家港市| 雅江县| 大荔县| 博野县| 吉林市| 象山县| 兴城市| 石首市| 弋阳县| 兰溪市| 永新县| 铁岭市| 临潭县| 博乐市| 屯昌县| 新丰县| 浦东新区| 辛集市| 乌鲁木齐县| 佛教| 噶尔县| 钟祥市| 额尔古纳市| 杭州市| 左贡县| 南投县| 安吉县| 洞口县| 丹巴县| 海原县| 运城市|