为国读书
2012-09-25 22:49
采纳率: 33.3%
浏览 300
已采纳

commons-fileupload+struts2报错

很奇怪的问题,在本地测试的从来没有报这个错误,上传服务器之后竟然一直报这个错误,不过上传也是成功的。

 

报错代码:

 

[Log] 2012-09-25 22:09:23,426 - ERROR -35293923 [catalina-exec-16] com.opensymphony.xwork2.util.logging.commons.CommonsLogger.error(CommonsLogger.java:27) - Unable to parse request
org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:367)
at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:93)
at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:75)
at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:663)
at org.apache.struts2.dispatcher.FilterDispatcher.prepareDispatcherAndWrapRequest(FilterDispatcher.java:327)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:367)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.shaiyaya.filter.CharacterAllEncodingFilter.doFilter(CharacterAllEncodingFilter.java:95)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.shaiyaya.filter.LoginFilter.doFilter(LoginFilter.java:35)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:354)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:85)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362)
... 28 more

 上传action:

 

String message;
        if (fileName == null) {
            message = "未上传图片!";
            request.setAttribute("message", message);
            return "emptyPic";
        }
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (",jpg,jpeg,gif,bmp,png,".indexOf("," + extension.toLowerCase()
                + ",") < 0) {
            message = "只允许上传jpg,jpeg,gif,bmp,png格式的图片";
            request.setAttribute("message", message);
            return "errorType";
        }
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "timeOut";
        }
        if (upload.length() > 2048000) {
            message = "图片大小超过了2M";
            request.setAttribute("message", message);
            return "overSize";
        }
        String baseDir = "/AVATAR_IMG/";
        String realBaseDir = ServletActionContext.getServletContext()
                .getRealPath(baseDir);
        File baseFile = new File(realBaseDir);
        if (!baseFile.exists()) {
            baseFile.mkdir();
        }
        String fileFolder = new SimpleDateFormat("yyyyMMdd").format(new Date());// 按天存入
        /* 文件存储在容器中的绝对路径 */
        String saveFilePath = ServletActionContext.getServletContext()
                .getRealPath(baseDir + fileFolder + "/");
        File fileDir = new File(saveFilePath);
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        /* 重命名文件 */
        String random = UUID.randomUUID().toString();
        fileName = random + "." + extension;
        
        BufferedImage oldBufferedImage = ImageIO.read(upload);
        int old_width = oldBufferedImage.getWidth();
        int old_height = oldBufferedImage.getHeight();
        int new_width = 100;
        int new_height = 100;  
        int new_width2 = 40;
        int new_height2 = 40;
        int new_width3 = 25;
        int new_height3 = 25;
        
        //100x100
        double sx = (double) new_width / old_width;
        double sy = (double) new_height / old_height;
        // 等比缩放
        if (sx > sy) {
            sx = sy;
            new_width = (int) (sx * old_width);
        } else {
            sy = sx;
            new_height = (int) (sy * old_height);
        }
        BufferedImage newBufferedImage;
        FileOutputStream bioNew;
        JPEGImageEncoder encoder;
        File newFile;
        try{
            newFile= new File(saveFilePath + "/" + fileName);
            newBufferedImage = new BufferedImage(new_width, new_height,BufferedImage.TYPE_INT_RGB);
            newBufferedImage.getGraphics().drawImage(
                oldBufferedImage.getScaledInstance(new_width, new_height,Image.SCALE_AREA_AVERAGING), 0, 0, null);
            bioNew = new FileOutputStream(newFile); 
            encoder = JPEGCodec.createJPEGEncoder(bioNew);
            encoder.encode(newBufferedImage);// 进行编码
        }catch(Exception e){
            log.error("upload avatar error 100x100:"+e.getMessage());
        }
        
        //45x45
        sx = (double) new_width2 / old_width;
        sy = (double) new_height2 / old_height;
        if (sx > sy) {
            sx = sy;
            new_width2 = (int) (sx * old_width);
        } else {
            sy = sx;
            new_height2 = (int) (sy * old_height);
        }
        try{
            newFile = new File(saveFilePath + "/" + fileName+"_40x40.jpg");
            newBufferedImage = new BufferedImage(new_width2, new_height2,BufferedImage.TYPE_INT_RGB);
            newBufferedImage.getGraphics().drawImage(
                oldBufferedImage.getScaledInstance(new_width2, new_height2,Image.SCALE_AREA_AVERAGING), 0, 0, null);
            bioNew = new FileOutputStream(newFile); 
            encoder = JPEGCodec.createJPEGEncoder(bioNew);
            encoder.encode(newBufferedImage);
        }catch(Exception e){
            log.error("upload avatar error 45x45:"+e.getMessage());
        }
        
        //25x25 
        if (new_width3 > old_width) {
            new_width3 = old_width;
        }
        if (new_height3 > old_height) {
            new_height3 = old_height;
        }
        try{
            newFile = new File(saveFilePath + "/" + fileName+"_25x25.jpg");
            newBufferedImage = new BufferedImage(new_width3, new_height3,BufferedImage.TYPE_INT_RGB);
            newBufferedImage.getGraphics().drawImage(
                oldBufferedImage.getScaledInstance(new_width3, new_height3,Image.SCALE_AREA_AVERAGING), 0, 0, null);
            bioNew = new FileOutputStream(newFile); 
            encoder = JPEGCodec.createJPEGEncoder(bioNew);
            encoder.encode(newBufferedImage);// 进行编码
            bioNew.close();
        }catch(Exception e){
            log.error("upload avatar error 25x25:"+e.getMessage());
        }
        
        储存...
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • jinnianshilongnian 2012-09-26 20:52
    已采纳

    [url]http://stackoverflow.com/questions/1586843/apache-commons-fileupload-tomcat-cant-cope-with-out-flush[/url]

    你服务器上的tomcat版本更新到至少6.0.20 试试。估计你本地是高版本

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • henghanan 2012-09-25 23:19

    1、是不是用的双服务器,而且之间有ip映射
    2、负载平衡和Tomcat的问题,你绕过负担均衡和直接访问Tomcat服务器的IP地址看看能不能解决

    评论
    解决 无用
    打赏 举报
  • iteye_5246 2012-09-26 09:02

    貌似前台页面jsp
    from 没有 enctype="multipart/form-data"属性

    评论
    解决 无用
    打赏 举报
  • iteye_5246 2012-09-26 09:19

    我错了,关键是:[ Stream ended unexpectedly ] 这个提醒我了。。
    应该是你而是由于上传以后,关闭了窗口或者刷新页面,导致了上传异常
    Processing of multipart/form-data request failed

    评论
    解决 无用
    打赏 举报
  • fang_fumin 2012-09-26 10:38

    struts2上传文件会有一个写文件的临时目录,是否那个目录为只读的

    评论
    解决 无用
    打赏 举报
  • ggxin 2014-02-17 11:43

    我也遇到了同样的问题啊,本地都可以,到了服务器上有部分文件可以 部分不可以,也出现断开连接,求解决

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题