webwork上传异常

我做webwork 的上传时候,上传功能正常实现,可是控制台输出异常信息:

java.lang.IllegalStateException: getReader() or getInputStream() called 

请问这个信息是什么意思啊,什么原因导致出现这个错误的?

 

 
09:27:42.906 INFO   [SocketListener0-0] com.opensymphony.webwork.dispatcher.DispatcherUtils.getSaveDir(DispatcherUtils.java:359) >16> Unable to find 'webwork.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir    
09:27:43.359 ERROR! [SocketListener0-0] 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)    
 at com.opensymphony.webwork.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:160)    
 at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:821)    
 at com.opensymphony.webwork.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:88)    
 at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:821)    
 at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:471)    
 at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)    
 at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)    
 at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:633)    
 at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)    
 at org.mortbay.http.HttpServer.service(HttpServer.java:909)    
 at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)    
 at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)    
 at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)    
 at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:245)    
 at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)    
 at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)    
   
====新建 文本文档.txt    
===================C:\DOCUME~1\Z\LOCALS~1\Temp\Jetty_127_0_0_1_8080__\upload__146e3062_112ac37bbfb__8000_00000001.tmp    
===================新建 文本文档.txt    
09:27:48.437 INFO   [SocketListener0-0] com.opensymphony.webwork.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:242) >22> Removing file up C:\DOCUME~1\Z\LOCALS~1\Temp\Jetty_127_0_0_1_8080__\upload__146e3062_112ac37bbfb__8000_00000001.tmp    
 

 

 

以下是Action代码:

 

 

 
public class FileUploadAction extends ActionSupport implements ModelDriven{    
   
    private File up;    
    private String upContentType;    
    private String upFileName;    
    private String filepath;    
    private FileUploadService fileuploadService;    
    private FileUpload fileinfo = new FileUpload();    
         
   
 public String execute() throws Exception {    
  if(up != null){    
  System.err.println("===="+upFileName);    
  FileUploadAction(up,upFileName);    
  }    
  return SUCCESS;    
 }    
 private String fileFileName;    
 public String FileUploadAction(File up,String upFileName) {    
   
  // 以下为保存文件,视情况修改    
  if (up.isFile()) {    
   //fileFileName = name;    
//   filename = file.getName();    
   fileFileName = upFileName;    
   FileOutputStream outputStream;    
   filepath = "E:/uploadfolder/";    
   System.out.println("==================="+up);    
   System.out.println("==================="+fileFileName);    
   fileinfo.setFileFilename(fileFileName);    
   fileinfo.setFilepath(filepath);    
   fileuploadService.insertFileInfo(fileinfo);    
   try {    
    outputStream = new FileOutputStream(filepath + fileFileName);    
   
    FileInputStream fileIn = new FileInputStream(up);    
   
    byte[] buffer = new byte[1024];    
   
    int len;    
   
    try {    
     while ((len = fileIn.read(buffer)) > 0) {    
      outputStream.write(buffer, 0, len);    
     }    
    } catch (IOException e) {    
   
     e.printStackTrace();    
    }    
   
    try {    
     fileIn.close();    
     outputStream.close();    
    } catch (IOException e) {    
   
     e.printStackTrace();    
    }    
   
   } catch (FileNotFoundException e) {    
   
    e.printStackTrace();    
   }    
   
  }    
  return fileFileName;    
 }    
 public void setUp(File up) {    
  this.up = up;    
 }    
 public void setUpContentType(String upContentType) {    
  this.upContentType = upContentType;    
 }    
 public void setUpFileName(String upFileName) {    
  this.upFileName = upFileName;    
 }    
 public Object getModel() {    
      
  return fileinfo;    
 }    
 public void setFileuploadService(FileUploadService fileuploadService) {    
  this.fileuploadService = fileuploadService;    
 }    
                  
} 
   

 


      这个问题曾发到过论坛里,可是被评为隐藏贴,还扣了我30分......到现在不知道为什么被评为隐藏......谁知道的话也帮我看看原因吧,我先谢过了。

 

转自:http://simpledev.iteye.com/blog/224818 

webwork上传异常分析:getReader() or getInputStream() call

在使用webwork-2.2.4.jar,xwork-1.2.1.jar时文件上传发生异常.

Java代码 复制代码
  1. 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.   
  2. java.lang.IllegalStateException: getReader() or getInputStream() called   
  3.  at org.mortbay.jetty.servlet.ServletHttpRequest.setCharacterEncoding(ServletHttpRequest.java:602)   
  4.  at javax.servlet.ServletRequestWrapper.setCharacterEncoding(ServletRequestWrapper.java:105)   
  5.  at com.opensymphony.webwork.dispatcher.DispatcherUtils.prepare(DispatcherUtils.java:392)   
  6.  ...  
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

Java代码 复制代码
  1. String encoding = null;   
  2.        if (Configuration.isSet(WebWorkConstants.WEBWORK_I18N_ENCODING)) {   
  3.            encoding = Configuration.getString(WebWorkConstants.WEBWORK_I18N_ENCODING);   
  4.        }   
  5. Locale locale = null;   
  6.        if (Configuration.isSet(WebWorkConstants.WEBWORK_LOCALE)) {   
  7.            locale = LocalizedTextUtil.localeFromString(Configuration.getString(WebWorkConstants.WEBWORK_LOCALE), request.getLocale());   
  8.        }   
  9.   
  10.        if (encoding != null && !MultiPartRequest.isMultiPart(request)) {   
  11.            try {   
  12.                request.setCharacterEncoding(encoding);   
  13.            } catch (Exception e) {   
  14.                LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);   
  15.            }   
  16.        }  
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

Xml代码 复制代码
  1. webwork.locale=en_US  
  2. 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位字符为文件名,可以避免上传后存在文件名同名情况

Java代码 复制代码
  1. int idx = file4FileName.indexOf(".");   
  2. String suffix = fileFileName.substring(idx);   
  3. System.out.println(suffix);   
  4. String prefix = org.apache.commons.lang.RandomStringUtils.randomAlphabetic(10);   
  5. String filename = prefix + suffix;   
  6. System.out.println(filename);   
  7. 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),

Java代码 复制代码
  1. String filename2 = new String(fileFileName.getBytes(),"UTF-8");   
  2. System.out.println("filename2:"+filename2);   
  3. fileFileName = filename2;  
String filename2 = new String(fileFileName.getBytes(),"UTF-8"); System.out.println("filename2:"+filename2); fileFileName = filename2;
 
 

 
将该问题解决。(注:默认已使用webwork上传图片而出现上面异常时分析,具体上传代码略)

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值