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

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 试试。估计你本地是高版本

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示