为国读书 2012-09-25 14:49 采纳率: 0%
浏览 318
已采纳

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 12:52
    关注

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

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

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

报告相同问题?

悬赏问题

  • ¥15 pbootcms多选调用成列表
  • ¥15 请帮我解决一下问题,学生不是工作者
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
  • ¥15 JNA 方法调用.dll异常
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部