The Java components SmartUpload and fileUpload implement the file upload function
This paper shares the specific code of Java component to realize the file upload function for your reference. The specific contents are as follows
1 SmartUpload upload component
SmartUpload upload component package, which can easily upload and download files;
It is easy to use and realize the restriction of uploaded file type. It can also easily obtain the name, suffix, size, etc. of uploaded file;
SmartUpload itself is a jar package provided by the system. Test this package into the Lib folder;
The provider website of this component has been closed, and SmartUpload is easy to use in non framework development;
Upload a single file
To upload, you must use the file space provided in HTML, and < form > must be encapsulated with enctype attribute;
smartupload_ demo01. HTML: upload form
The form is encapsulated with enctype on the form, which means that the form will be submitted in binary mode, that is, all forms will not be submitted separately at this time, but all contents will be submitted in binary mode;
smartupload_ demo01. Jsp: receive the picture and save it in the upload folder in the root directory
When using SmartUpload, you must strictly follow the above procedures. Finally, when saving, only an upload is written, indicating the saving folder of the uploaded file. This file should be manually created in the root directory;
The saved name is the same as the uploaded file, so if the same file name appears, it will be overwritten;
Mixed form
When a form is encapsulated with enctyoe, the contents of form controls of other file classes cannot be obtained through the request built-in object;
At this time, all request parameters must be obtained through the getrequest() method provided in the SmartUpload class;
smartupload_ demo02. html ; Mixed form
The above form contains two controls: text and file;
smartupload_ demo02. Jsp: receives the text data that encapsulates the form
The form is binary encapsulated. The submission parameters cannot be obtained by the request object alone. You must rely on getrequest() in the SmartUpload class The getparameter () method can obtain the requested parameters;
Since the parameters are received through SmartUpload, smart The getrequest () method can only be used after the upload () method is executed;
Automatically name uploaded files
In order to solve the problem of overwriting due to the same file name, the method of automatic naming for uploaded files can be adopted;
The format of automatic naming can be: IP address + timestamp + three digit random number
IPTimeStamp. Java: defines the operation class to obtain the IP timestamp
Directly modify the uploaded operation page, and do not directly use the save() method to save the uploaded operation page;
Instead, a specific uploaded file object can be saved. Since the file suffix needs to be unified when uploading a file, you can use the getfileext () method to obtain the file suffix;
smartupload_ demo02. Jsp: add the function of automatic naming;
Because SmartUpload can receive multiple files at the same time;
At this time, through smart getFiles(). getFile(0). Getfileext() obtains the file suffix of the first uploaded file, and pieces together a new file name with the file name generated by the iptimestampl class before;
At this time, you need to save the uploaded file with a new file name, so you need to use smart getFiles(). getFile(0). Saveas() method to save after work;
The above code does not limit the type of uploaded file. You can judge whether the suffix of the file is legal through regularization;
Eg: code fragment for verifying the legitimacy of the uploaded file
Batch upload
smart. getFiles(). getFile(0). saveAs(this.getServletContext(). getRealPath("/")+"upload"+java. io. File. separator + fileName) ; The above certificates can submit multiple uploaded files at one time;
smartupload_ demo03. Jsp: write a form and upload three files
If batch uploading is to be completed, it must be carried out in a circular way. The number of uploaded files must be obtained in the following ways;
Get the number of uploaded files: smart getFiles(). getCount();
smartupload_ demo03. Jsp: batch upload
2 FileUpload
FileUpload is a free upload component provided by Apache organization, which can be downloaded directly from the site( http://commons.apache.org/fileupload/ );
The fileUpload component itself depends on the Commons component package, so download this component from Apache together with the IO package of the Commons component( http://commons.apache.org/io );