0

I am learning Upload interceptor now. The document says, all upload files size exceeds the value of constant "struts.multipart.maxSize" will throw a exception in the background, and the view will go to INPUT view. But, in my case,it can not throw any exception, and the view did not go to INPUT view(Upload progress has been 0% always,see the picture).

enter image description here

this is my upload html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>HELLO!${session.user }</h1>
    <div align="center">
        <s:form method="post" enctype="multipart/form-data" action="upload">
        <s:fielderror></s:fielderror>
        <s:file label="上传文件" name="upload" />
        <s:file label="上传文件" name="upload" />
        <s:file label="上传文件" name="upload" />
        <s:submit />
        </s:form>
    </div>
</body>
</html>

this is the code of my Action

package com.aks.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.UUID;


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{


    /**
     * 
     */
    private static final long serialVersionUID = -6671906825564499267L;

    private static final int BUFFER_SIZE = 4096;

    private List<File> upload;
    private List<String> uploadFileName;
    private List<String> uploadContentType;
    private String savePath;

    public String getSavePath() {
        return ServletActionContext.getRequest().getServletContext().getRealPath(this.savePath);
    }


    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }


    public List<File> getUpload() {
        return upload;
    }


    public void setUpload(List<File> upload) {
        this.upload = upload;
    }


    public List<String> getUploadFileName() {
        return uploadFileName;
    }


    public void setUploadFileName(List<String> uploadFileName) {
        this.uploadFileName = uploadFileName;
    }


    public List<String> getUploadContentType() {
        return uploadContentType;
    }


    public void setUploadContentType(List<String> uploadContentType) {
        this.uploadContentType = uploadContentType;
    }


    @Override
    public String execute() throws Exception {
        String newFileName = (UUID.randomUUID() + uploadFileName.get(0).substring(uploadFileName.get(0).lastIndexOf(".")));
        System.out.println(newFileName);
        System.out.println(getSavePath());
        FileInputStream fis = new FileInputStream(upload.get(0));
        FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + newFileName);
        System.out.println(getSavePath());
        FileChannel fcIn = fis.getChannel();
        FileChannel fcOut = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
        while(true){
            buffer.clear();
            int r = fcIn.read(buffer);
            if(r == -1){
                break;
            }
            buffer.flip();
            fcOut.write(buffer);
        }
        getUploadFileName().set(0, newFileName);
        fcIn.close();
        fcOut.close();
        fis.close();
        fos.close();
        return SUCCESS;
    }
}

this is the result html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示上传图片</title>
</head>
<body>
    <img src="upload/${uploadFileName['0']} " />
    <s:debug></s:debug>
</body>
</html>

At last, this is the struts.xml of this action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.multipart.maxSize" value="100000000" /> <!-- 20MB -->
    <constant name="struts.multipart.saveDir" value="/temp" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="index">
            <result name="success">index.jsp</result>
        </action>
        <action name="login" class="com.aks.action.LoginAction">
            <result name="input">index.jsp</result>
            <result name="success">/WEB-INF/jsp/welcome.jsp</result>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </action>
        <action name="upload" class="com.aks.action.UploadAction">
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.allowedTypes">image/png,image/jpeg,application/x-zip-compressed,application/octet-stream</param>
                <param name="fileUpload.maximumSize">90000000</param>
            </interceptor-ref>
            <param name="savePath">/upload</param>
            <result name="input">index.jsp</result>
            <result name="success">/WEB-INF/jsp/showImage.jsp</result>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </action>
    </package>


    <!-- Add packages here -->

</struts>

My development environment: eclipse4.5.1+tomcat8+jdk1.8+struts2.3.24

Why can not throw exception,why not go to Input view?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Ran Qilin
  • 1
  • 3
  • why don't you use try catch block...and when exception is caught return INPUT,When exception is caught it will return to same page in struts2 i believe.http://www.mkyong.com/struts2/struts-2-file-upload-example/ – goodyzain Jan 04 '16 at 09:14
  • but there is not any exception.that means I can not catch any exception.and in the command line of tomcat, there is not print any exception. – Ran Qilin Jan 05 '16 at 01:13
  • @BalusC, you give the demo I have seen.In your demo used "fileUpload.maximumSize" to limit the size. When I used fileUpload.maximumSize to limit the size is not problem.It means when fileUpload.maximumSize < my file size < struts.multipart.maxSize, there is not problem, it can throw exception and go to INPUT view.However, in this case, when fileUpload.maximumSize < struts.multipart.maxSize < my file size, that means my file size exceeds struts.multipart.maxSize, the problem appears. – Ran Qilin Jan 05 '16 at 01:29
  • by the way,my browser is Chrome – Ran Qilin Jan 07 '16 at 00:59

1 Answers1

0

This is a known problem, analyzed in this answer, and (as you can read in the update), it's been "patched" in Struts2 2.3.18.

Just upgrade to 2.3.24.1 and use jakarta-stream as Multipart parser.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243