Detailed explanation of struts 2 file upload in Java

The first is the web page, upload_ file. jsp

For the form of uploading files, the metho must be set to post, and the enctype must be set to multipart / form data.

From the above code, we can see that this form is submitted to the UploadFile action for processing. Then we will use struts XML is configured as follows:

The fileUpload interceptor is used to set the upload path and limit the file type and size.

As for limiting the file size, it is not enough to have < param name = "maximumsize" > alone. It must also be added under the < struts > tag

This line of code represents the maximum file size allowed to be uploaded in all places of the whole project, In other words, the size of any single file uploaded in this project cannot exceed 21000000 bytes (about 20m). If this line of code is not added to the project, the maximum file size allowed to be uploaded by default is 2m, so this is also a way to break through the limitation that struts 2 can only upload 2m files.

For limiting file types, < interceptor > can be configured in this way if you need to limit them to image files

The values in the < param name = "allowedtypes" > tag are the MIME types of files. The MIME types of common files can be found in% Tomcat_ HOME%\conf\web. Found in XML.

If you want to restrict to word files, you can configure < interceptor > this way

However, I feel that it is better to limit the file type in this way than to implement the limitation in the front end with JavaScript.

Next, write uploadaction. The required private attribute of uploadaction is source, which is the same as upload_ file. The name attribute of the file field in the JSP is consistent, that is, if the name attribute value of the file field is source, there must be a private attribute source in the uploadaction. In addition, there are two important private attributes:

private String sourceFileName; // File name of the file to be uploaded private string sourcecontenttype// The file type of the file to be uploaded. The format of these two variable names is the prefix source and upload_ file. The name attribute of the file field in JSP is the same.

Generally speaking, it is, such as upload_ file. If the name of the file field in JSP is = "ABC", it needs to be defined in action

ABC will automatically obtain the file object to be uploaded, abcfilename will automatically obtain the file name, and abccontenttype will automatically obtain the file type.

I want to focus on the upload path.

It's good to upload to the absolute path, but how to get the full path of the upload folder if you want to upload to the upload folder under the project root directory?

I tried to use it

ServletActionContext. getServletContext(). getRealPath("/upload"); But null was returned. Also used

ServletActionContext. getRequest(). getRealPath("/upload"); Still returned null. However, after checking this problem on the Internet, many people recommend it, which proves that it may be feasible to write it in some cases, but there are also people who return null like me. They also recommend a new method, that is, let uploadaction implement the servletcontextaware interface. The specific methods are as follows:

Then use

Get the path to the upload folder. Then perform upload:

I personally recommend this method, because it seems to avoid ensuring the correct path when the project is packaged and transferred to other environments.

The complete code of uploadaction is pasted later java

Upload result page uploadresult jsp

The above is the whole content of this article. I hope you can like it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>