使用jakarta commons-upload。
升级到2.2.2后发现上传中文会乱码。
经过跟踪发现在com.opensymphony.webwork.dispatcher.DispatcherUtils的prepare(HttpServletRequest request, HttpServletResponse response)方法。
2.2.1->2.2.2时这个方法发生了非常的的变化。
2.2.1时:
public void prepare(HttpServletRequest request, HttpServletResponse response) {
if (encoding != null) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
}
}
if (locale != null) {
response.setLocale(locale);
}
if (paramsWorkaroundEnabled) {
request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
}
}
2.2.2时:
public void prepare(HttpServletRequest request, HttpServletResponse response) {
String encoding = null;
if (Configuration.isSet(WebWorkConstants.WEBWORK_I18N_ENCODING)) {
encoding = Configuration.getString(WebWorkConstants.WEBWORK_I18N_ENCODING);
}
Locale locale = null;
if (Configuration.isSet(WebWorkConstants.WEBWORK_LOCALE)) {
locale = LocalizedTextUtil.localeFromString(Configuration.getString(WebWorkConstants.WEBWORK_LOCALE), request.getLocale());
}
if (encoding != null && !MultiPartRequest.isMultiPart(request)) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
}
}
if (locale != null) {
response.setLocale(locale);
}
if (paramsWorkaroundEnabled) {
request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
}
}
我看了jira没有发现encoding != null && !MultiPartRequest.isMultiPart(request)这个判断的意义。但是它会造成对multiPartRequest的encoding判断失败,中文就会乱码。
所以临时的解决方案是将判断改成:
if (encoding != null)
听说WW的Jira已经不接受新issue了,不知是否真的?