Upload struts file

The basic jar package of struts has provided the related jar package commons-fileupload.jar for File upload, which is used to encapsulate the uploaded File as File type

Here is a simple example to describe the steps of file upload in struts

1. First, create the struts project according to the steps of creating the struts project

2. Write an UploadAction.java

public class UploadAction extends ActionSupport {
    private File photo;
    public String upload(){
        System.out.println(photo);
        return NONE;
    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }
}

3. Write an upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>file_upload</title>
</head>
<body>
    <s:form enctype="multipart/form-data" action="upload" method="POST">
        <s:file label="Photo" name="photo"></s:file>
        <s:submit label="upload"></s:submit>
    </s:form>
</body>
</html>

4. Set action in struts.xml file

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="upload" extends="struts-default" namespace="/file">
        <action name="upload" class="action.UploadAction" method="upload">
            <result name="input">/upload.jsp</result>
        </action>
    </package>
</struts>

Start tomcat to access the upload.jsp path, select the file, and click Submit

The console prompts are as follows

ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'photoContentType' on 'class action.UploadAction: Error setting expression 'photoContentType' with value ['image/png', ]
ERROR ParametersInterceptor Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'photoFileName' on 'class action.UploadAction: Error setting expression 'photoFileName' with value ['Annotation 2020-01-11 114441.png', ]
C:\Users\User name\.IntelliJIdea2019.3\system\tomcat\Tomcat_8_5_31_upload_struts\work\Catalina\localhost\upload_struts_war_exploded\upload_51ef87c8_b4dd_4c14_80b3_63b2fe6d7a1a_00000000.tmp

This will automatically encapsulate the file as a file type. There is a temporary folder, eliminating the previous byte stream of the read file.

However, there are two errors in this. Look at the information output from the above console. According to the prompts, we find that there are two attributes missing in action, one is the file type, the other is the photoFileName (note that the name attribute in the form will be changed according to the value of the name attribute in the < s: File > < s: File > tab. If the value of the name attribute is xxx, Then the preceding property names are xxxContentType and xxxFileName)

5. Eliminate the error and add the following two attributes to the action

public class UploadAction extends ActionSupport {
    private String photoContentType;
    private String photoFileName;
    private File photo;
    public String upload(){
        System.out.println(photo);
        System.out.println(photoContentType);
        System.out.println(photoFileName);
        return NONE;
    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }
}

    

Submit the file again, and the console output information is as follows

C:\Users\User name\.IntelliJIdea2019.3\system\tomcat\Tomcat_8_5_31_upload_struts\work\Catalina\localhost\upload_struts_war_exploded\upload_8232872a_3647_4d48_9e2a_9888099d7a4e_00000000.tmp
image/png
//Annotation 2020-01-11 114441.png

6. The following describes how to transfer the auto encapsulated file to the server

public class UploadAction extends ActionSupport {
    private String username;
    private String photoContentType;
    private String photoFileName;
    private File photo;
    public String upload(){
        System.out.println(photo);
        System.out.println(photoContentType);
        System.out.println(photoFileName);
        //1. Get the storage directory
        String dictory = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload")+"/" +username;
        System.out.println("Save path"+dictory);
        File f=new File(dictory);
        if (f.exists()==false){
            f.mkdirs();
        }
        //2. File saving path
        String fileSavePath=dictory+"/"+photoFileName;

        //3. Cut the file to another location
        //FileUtils.copyFile(photo,new File(fileSavePath));
        photo.renameTo(new File(fileSavePath));
        return NONE;

    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }
}

A text field named username is added in upload.jsp to distinguish folders of different users

<s:form enctype="multipart/form-data" action="/file/upload" method="POST">
        <s:textfield label="User name" name="username"></s:textfield>
        <s:file label="Photo" name="photo"></s:file>
        <s:submit label="upload"></s:submit>
</s:form>

Visit the page, select the file, input the user name, click Submit, the file already exists in the WEB-INF of the project of the server

matters needing attention:

struts provides built-in tags for file upload < s: File >, which we call file selection domain

Set a form with the enctype of multipart / form data and the enctype of encoding format

multipart/form-data

No character encoding.

This value must be used when using a form that contains a file upload control.

Form submission method must be post

You must set the two properties of photoContentType and photoFileName in the action of processing files, and add the set and get methods to them

 

Published 110 original articles, won praise 13, visited 2394
Private letter follow

Tags: Struts JSP encoding Tomcat

Posted on Thu, 16 Jan 2020 06:28:42 -0500 by spamyboy