Single file upload and multi file upload of struts 2

File upload refers to uploading the local file to the specified directory of the server and using struts to upload the file. First, set the value of the enctype attribute of the form form form to multipart / form data.

Single file upload

The code of file upload page is as follows

<body>
<s:actionerror />
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
    <input name="upload" type="file" /><br />
    <input type="submit" value="upload" />
</form>
</body>

File upload Action class

public class UploadAction extends ActionSupport {
    private File upload; //file
    private String uploadContentType;//file type
    private String uploadFileName;//file name
	//The get and set methods of three properties are omitted here

    public String upload() {
        // Set the path to save the file
        String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/");
            File f = new File(path+"/"+uploadFileName);
            try {
                FileUtils.copyFile(upload, f);
                System.out.println("Upload success");
            } catch (IOException e) {
            	//Set the error information and display it on the homepage when uploading the error
                this.addActionError("Upload failure");
                e.printStackTrace();
                //Return to page on error
                return "upload";
            }
        return null;
    }
}

Need to set an error jump page in struts.xml

 <action name="upload" method="upload" class="com.wang.action.UploadAction">
            <result name="upload">/Upload.jsp</result>
 </action>

Multi file upload

Multi file upload is the same as single file upload, that is, set more form boxes for several files on the uploaded page, and the values of the form name attribute should be the same

<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
    <input name="upload" type="file" /><br/>
    <input name="upload" type="file" />
    <input type="submit" value="upload" />
</form>

Then we need to replace the original single file operation with array operation in Action

public class UploadAction extends ActionSupport {
    private File[] upload; //file
    private String[] uploadContentType;//file type
    private String[] uploadFileName;//file name
	// The get and set methods of three properties are omitted here
	
    public String upload() {
        //Set the path to save the file
        String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload/");

        for (int i = 0; i <uploadFileName.length ; i++) {

            File f = new File(path+"/"+uploadFileName[i]);

            try {
                FileUtils.copyFile(upload[i], f);
                System.out.println("Upload success");
            } catch (IOException e) {
                this.addActionError("Upload failure");
                e.printStackTrace();
                return "upload";
            }

        }

        return null;
    }

Limit file size and type

Set constants in the struts.xml file

  • maximumSize: the maximum length of the uploaded file, which is 2MB by default
  • Allowed types: types of files that can be uploaded, separated by commas
  • Allowed extensions: extensions that allow files to be uploaded, separated by commas
<!--Set the maximum allowed file upload value to 10 MB(Setting global)-->
<constant name="struts.multipart.maxSize" value="10485760"></constant>

<!-- stay<action>Set interceptor in -->
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">4194304</param>
<param name="fileUpload.allowedTypes">
	text/plain,application/msword
</param>

<param name="allowedExtensions">
.txt,.png,.jpg
</param>
</interceptor-ref>

Tags: Struts Attribute xml JSP

Posted on Thu, 07 Nov 2019 13:43:07 -0500 by Keith Scott