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