Posted on 2005-12-05 10:47
Terry的Blog 閱讀(620)
評(píng)論(0) 編輯 收藏 所屬分類:
java語(yǔ)言
Struts1.0.2中上傳文件功能的Bug(日文文件名有時(shí)不能完整表示)
struts1.0.2中解析"multipart/form-data"型的request時(shí)沒(méi)有根據(jù)request.getCharacterEncoding()的結(jié)果來(lái)解碼.
當(dāng)截取filename時(shí)就可能丟失一些字符.比如文件名為"ソウス.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();
......
}
// 自定義一個(gè)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);
// 設(shè)置處理MultipartRequest的類,也可以在struts-config.xml中設(shè)置。
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);
}
}
}
這個(gè)問(wèn)題在struts1.1中得到了解決.