OneFile Upload
- File upload, that is, the server to get and process the user uploaded files, this file is stored in the request, that is, the request must be processed.
1.1 Writing HTML files
<! DOCTYPE html>enctype= "Multipart/form-data"> file Description: <input type= "Text" Name= "desc"/><br/> <input type= "file" name= "file"/><br/> <input type= " Submit "value=" Upload "/> </form></body>
Parsing: enctype= "Multipart/form-data" is an indispensable attribute for file upload, but after adding this attribute, it cannot be request.getparameter (arg0); To get the form value, only getfieldname () Gets the table sole name,getString () Gets the form value.
1.2 Writing a servletprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Request.setcharacterencoding ("UTF-8"); FileUpload (request);} /*
* File Upload
*/
public void FileUpload (HttpServletRequest request) {//Disk File factory class Diskfileitemfactory is mainly set temporary file directoryDiskfileitemfactory diff=new diskfileitemfactory ();d iff.setsizethreshold (10*1024*1024);//Set the critical value of the upload file size, which is greater than this value first exists in the temporary directory, and then uploaded to the server. Less than this value is uploaded directly to the server. 10MDiff.setrepository (New File (This.getservletcontext (). Getrealpath ("tmp")));//Set Temp file directory ///Temporary files are set up so that the uploaded files can be processed. Servletfileupload upload=new servletfileupload (diff);//File upload object upload according to the File factory diff configuration
/* Next you can parse the request, and the *request data is divided into ordinary form data and files, which need to be treated differently. * The list<fileitem> type is parsed for request, and a form value is an object. */List<fileitem> items=null;try {items=upload.parserequest (request);//Parse the request and parse it out of the list<fileitem> typefor (Fileitem Item:items) {//Loop through all the data in the form, then differentiate between the form data and the file dataif (Item.isformfield ()) {//If the form dataif ("desc". Equals (Item.getfieldname ())) {//If the element name of the form element is DESC,System.out.println ("The file is described as:" +item.getstring ("UTF-8"));//item.getstring ("UTF-8") solve garbled problem}}else{//Otherwise file dataString Path=this.getservletcontext (). Getrealpath ("Upload");//Get the real path to the folder where the uploaded files are stored (that is, the path on the hard disk, not the relative path in the project)OutputStream os=new FileOutputStream (New File (path+ "/" +item.getname ()));//The path of the user to upload the file is in the path folder, the name is Item.getname () is the file name at the time of uploading
if (Item.isinmemory ()) {//If the file is stored directly in memory, that is less than the critical value of the temporary file, less than 10M, you can directly copy the user uploaded files directly into the upload file. Os.write (Item.get ());//item.get () gets the data stream of the file uploaded by the user}else{//If the upload file size is greater than the critical value, this file will now be in the TEMP folder tmp and then uploaded to the upload folder. Ioutils.copy (Item.getinputstream (), OS);//directly copy the data stream of user files to the output stream}os.close ();}}} catch (Exception e) {e.printstacktrace ();}}
Right-click the project name, select "Properties",
Open path: "C:\Users\Administrator\workspace" in the diagram and select the first folder
Find this Folder
Find the project name and click to enter:
You can find the upload or TMP folder.
(vii) Use Apache components for file upload and download