在使用webwork-2.2.4.jar,xwork-1.2.1.jar时文件上传发生异常.
- 00:29:07.390 ERROR! [SocketListener0-1] com.opensymphony.webwork.dispatcher.DispatcherUtils.prepare(DispatcherUtils.java:394) >17> Error setting character encoding to 'UTF-8' - ignoring.
- java.lang.IllegalStateException: getReader() or getInputStream() called
- at org.mortbay.jetty.servlet.ServletHttpRequest.setCharacterEncoding(ServletHttpRequest.java:602)
- at javax.servlet.ServletRequestWrapper.setCharacterEncoding(ServletRequestWrapper.java:105)
- at com.opensymphony.webwork.dispatcher.DispatcherUtils.prepare(DispatcherUtils.java:392)
- ...
00:29:07.390 ERROR! [SocketListener0-1] com.opensymphony.webwork.dispatcher.DispatcherUtils.prepare(DispatcherUtils.java:394) >17> Error setting character encoding to 'UTF-8' - ignoring. java.lang.IllegalStateException: getReader() or getInputStream() called at org.mortbay.jetty.servlet.ServletHttpRequest.setCharacterEncoding(ServletHttpRequest.java:602) at javax.servlet.ServletRequestWrapper.setCharacterEncoding(ServletRequestWrapper.java:105) at com.opensymphony.webwork.dispatcher.DispatcherUtils.prepare(DispatcherUtils.java:392) ...
分析源码如下:
com.opensymphony.webwork.dispatcher.DispatcherUtils.java:394
- 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);
- }
- }
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); } }
webwork.properties
- webwork.locale=en_US
- webwork.i18n.encoding=UTF-8
webwork.locale=en_US webwork.i18n.encoding=UTF-8
文件上传经过控制层处理时,得到的文件file对象,文件类型ContentType,文件名FileName都是正确的,文件名中文显示正常.
为了避免上面webwork抛出的异常,我们可以使用低版本的webwork(webwork-2.2.2.jar,xwork-1.1.3.jar)
运行程序之后系统没有抛出"java.lang.IllegalStateException: getReader() or getInputStream() called",但是文件名如果是中文为乱码,
为了解决文件名中文乱码问题,我们可以采取两种简单的处理方法:
//方法一:获取文件后缀名
//优点:使用RandomStringUtils随机生成10位字符为文件名,可以避免上传后存在文件名同名情况
- int idx = file4FileName.indexOf(".");
- String suffix = fileFileName.substring(idx);
- System.out.println(suffix);
- String prefix = org.apache.commons.lang.RandomStringUtils.randomAlphabetic(10);
- String filename = prefix + suffix;
- System.out.println(filename);
- fileFileName = filename;
int idx = file4FileName.indexOf("."); String suffix = fileFileName.substring(idx); System.out.println(suffix); String prefix = org.apache.commons.lang.RandomStringUtils.randomAlphabetic(10); String filename = prefix + suffix; System.out.println(filename); fileFileName = filename;
//方法二:字符串转码
//使用上传的文件名,将乱码中文名转码(UTF-8),
- String filename2 = new String(fileFileName.getBytes(),"UTF-8");
- System.out.println("filename2:"+filename2);
- fileFileName = filename2;
String filename2 = new String(fileFileName.getBytes(),"UTF-8"); System.out.println("filename2:"+filename2); fileFileName = filename2;
将该问题解决。(注:默认已使用webwork上传图片而出现上面异常时分析,具体上传代码略)