File upload is a very common feature, today say struts2 file upload, using the FileUpload component.
The first step is to have two jar packages, as shown in:
If you do not have the two jar package, the upload will certainly not succeed, the console would be error.
Then on the page you want to have a tag to upload the file, as shown in:
Do not care about the back of the onchange event, this is to do a picture upload, in order to be able to click the image directly pop-up file selection box and write a function, by the way, the specific functions and practices as shown:
Here is the function:
This one... In the following, continue to say that the file upload, the page upload file label most important is type= "file" and the label Name property, because in action to use the value of this name property.
In action, you give the property values of the following three files, and provide the Set/get method.
As you can see, the label's Name property value is used, and the subsequent imafilename and Imacontenttype must start with the file name, or they will not get the value.
Then there is the specific upload section, the action method is as follows:
The general approach is this, where
is to set the path you want to save the file, placed under the Web-inf is to ensure the security of the file, of course you can also be placed directly under the project root directory, you can also choose Folders, the following three lines of code is to judge your project
If there is a folder where you want to save the file, create a new one if it doesn't exist.
This part is the use of I/O stream to read and write files, there is nothing to say, remember the last to close the input and output stream.
To this a file upload basically completed, but someone will ask, if I only upload Excel file or Word file to do, or I only ask to upload files less than 10M what to do?
This problem can be judged in the method in the upload action, or in the Struts2 configuration file, as specified in the Struts2 file below.
Someone saw this and said, "Is this not a struts2 interceptor?" Right, is the interceptor, the interceptor set param parameters to implement the upload, Allowedtypes is allowed to upload the type,
MaximumSize is the size of the file that is allowed to upload. Of course you can also put the upload path of your files here.
In addition to interceptors, you should also write these two lines of code in the STRUTS2 configuration file:
This note is all written, and I'll stop nagging.
At this time you can upload files, but the upload when the small hand shook, the choice of the picture selected a Word file, and then did not look on the upload, and then on the page except a sentence of English, carefully see, oh, the original file type is not Ah,
Fortunately, the friend of English over the six level, but, if the English has not been six level how to do? The following is not a six-level English. No, what happens when it happens?
You need two files to get it done. First add these two lines of code to the STRUTS2 configuration file:
is the need to join the International resource file, value is the name of your resource file, and then add the following code in the resource file to
Well, of course, you struts2. The value of the resource file in the configuration file is matched to the name of your resource file.
Well, this is a personal learning record, Daniel can skip, what wrong to welcome you to correct.
SSH framework fileupload File upload