Detailed description of Struts2 File Upload
I. Case study: Enter the form on the uploadfile. jsp page, submit the form, go to the success. jsp page, and verify that the file is successfully uploaded under the upload package.
Ii. Case study: Uploading struts2 files is not a simple configuration. There are actually two steps. 1. struts2 first saves the File Uploaded by the client to struts. multipart. the directory specified by the saveDir key. If the directory corresponding to the key does not exist, it will be saved to javax. servlet. context. the directory specified by the tempdir environment variable. 2. The File member variable defined in Action actually points to a temporary file in the temporary directory, then, the server writes temporary files to the specified server directory through IO.
A) first on the upload page (uploadfile. jsp definition form)
1. In the form label, the method is post and enctype = "multipart/form-data" is added"
2. The name defined by the input tag of the type file is used in the action. We define it as "file"
B) Create action (UploadFileAction. java)
1. to upload a file, we first need three member variables:
Private File file;
Private String fileFileName;
Private String fileContentType;
2. File Name retrieval rules: file name + FileName
3. file type acquisition rules: file name + ContentType
4. upload files through IO operations.
C) Configure struts. xml
D. The file name and type are displayed on the success. jsp page.
E) Analysis of struts2 File Upload principles
1. After the project is uploaded, you can view the console output and find:
Information: Unable to find 'struts. multipart. savedir' property setting. Defaulting to javax. servlet. context. tempdir
You can configure struts. multipart. saveDir from the previous chapter 'struts2's default. properties.
2. You can view the temporary file by outputting the temporary file path in the program.
3. After the file is uploaded successfully, it is deleted. Therefore, we can let the thread sleep to view the temporary file in the program.
F) If you want to know how to upload multiple files, you can use Baidu. This is very simple. I will not explain it here.
Iii. Experience:
A) through the previous blog "Detailed description of Struts2 Servlet File Upload", we will learn whether struts2 File Upload is easy to understand. After analysis, struts2 only encapsulates the underlying layer, making it easier to use.
B) the reason for not explaining in the above f is that we use multi-File Upload in the project, which is generally not implemented by ourselves. We generally use plug-ins for multi-file upload. In my website "aishang partners", image uploading in album is the plug-in used.
C) if you like The Multifile upload plug-in, you may not be able to use Baidu. You can contact me to help you implement the plug-in.
4. Reference project: http://download.csdn.net/detail/u011638419/8218863
5. happy every day:
I said that my nephew was in a car accident and flew out seven meters without being hurt.
It is because there is a huge bag on the back.
He said this was the first time he felt useful in reading books.
Knowledge changes fate!
The right to final interpretation belongs to partner aishang (www.23hhr.com). Please indicate the source for reprinting.