SSH framework FileUpLoad file upload,
File Upload is a very common function. Today, let's talk about the file upload of struts2, which uses the fileupload component.
First, you must have two jar packages, as shown in:
If you do not have these two jar packages, the upload will certainly fail, and the console will report an error.
Then there must be a file upload tag on the page, as shown in:
Don't care about the next onchange event. This is a function for uploading images. In order to be able to click the image and pop up the file selection box, let's mention it by the way, shows the specific functions and practices:
The following are functions:
This... Now let's get to the point where we continue to talk about file Upload. The most important tag for uploading files on the page is type = "file" and the name attribute of this tag, because the value of this name attribute is used in action.
In action, the attribute values of the following three files should be given and the set/get method should be provided.
Here we can see that the label name attribute value is used, and the subsequent imaFileName and imaContentType must both start with the file name, otherwise the value cannot be obtained.
Then the specific upload part is displayed. The action method is as follows:
The general method is as follows:
Is set you want to save the file path, put in the WEB-INF is to ensure the security of the file, of course you can also directly put in the project root directory, you can also choose your own folder, the following three lines of code determine your project
Whether the folder you want to save the file exists. If it does not exist, create a new one.
This part is to use the I/O Stream to read and write files. There is nothing to say. Remember to close the input/output stream at last.
The upload of this file is basically complete, but someone will ask, what if I only upload an Excel file or a word file, or if I only want to upload a file smaller than 10 MB?
You can determine this problem in the method of the uploaded action, or you can specify it in the struts2 configuration file. The following describes the method specified in the struts2 file.
Someone will say that this is not the struts2 interceptor? By the way, the interceptor sets the param parameter in the Interceptor to implement the upload. allowedTypes is the type that allows the upload,
MaximumSize is the size of the file that can be uploaded. Of course, you can also put the upload path of your file here.
In addition to the interceptor, you should also write the following two lines of code in the struts2 configuration file:
After all the comments have been written, I will not be embarrassed.
At this time, you can upload files, but when you upload the files, you shake your hands and select a word file for the image, and then upload the file without looking at it, on the page, there is only one sentence in English. Take a closer look. Oh, it turns out that the file type is incorrect,
Fortunately, my buddy has passed cet6, But what if cet6 has passed? In the following example, the English language has not passed cet6 ..... No. What should I do if this happens?
You need to fix these two files. Add the following two lines of code to the struts2 configuration file:
You need to add an international resource file. value is the name of your resource file, and then add the following code to the resource file.
In this case, the value Attribute of the source files in the struts2 configuration file must match the name of your resource file.
Well, this is a personal learning record. You can skip it. If you have any mistakes, please let us know.