21. File Upload/download

Source: Internet
Author: User

I. Upload and download the file to store the client (browser) Big data on the server side, do not store the data directly in the database, but to store the data on the disk where the server is located (reduce the pressure on the database server, the operation of the data more flexible) 1, the principle of File upload 1.1 Necessary prerequisites for File upload: (1) Provide form form, method must bePost(2) The enctype (encoding type) of form forms must beMultipart/form-data(3) Provide input type= "file" class ${pagecontext.request.contextpath}: Absolute path 1.2 Enctype property tells the server the MIME (file type) type of the request body. (Request message Header: Content-type action is consistent) optional value: application/x-www-form-urlencoded (default): Body: name=admin&password= 123 Server Fetch data: String name = Request.getparameter ("name"); Multipart/form-data: Body server Get Data: Request.getparameter (String) Method gets the specified form field character content, but the file upload form is no longer a character content, butbyte content, so it fails. file Upload essence: Parse the contents of each part of the request body   2, using third-party upload components for file uploads 2.1 fileupload Overview FileUpload is an upload component provided by the Apache Commons component. Its main job is to help us analyze Request.getinputstream ()   Import Commons-fileupload related jar package (1) Commons-fileupload.jar, core Pack (2) Commons-io.jar, dependency package  2.2 The core classes of FileUpload are: Diskfileitemfactory, servletfileupload, fileitem  parsing principle   2.3 FileUpload Simple applications use the FileUpload component as follows: (1) Create a factory class Diskfileitemfactory object Diskfileitemfactory factory = new Diskfileitemfactory () (2) Use factory to create parser object Servletfileupload fileUpload = new Servletfileupload (Factory) (3) Use the parser to parse the request object List<fileitem> list = fileupload.parserequest (request)  fileitem object corresponding to one form item (form field), Can be a file field or a normal field, Boolean Isformfield (): Determines whether the current form field is a normal text fields, if False, the description is a file field; String getfieldname (): Gets the field name, for example: <input Type= "text" name= "username"/&GT; return username;string getString (): Gets the contents of the field, if it is a file field, then gets the file content, of course, the uploaded file must be a text file String GetName (): Gets the file name of the file field, (A.txt) string getContentType (): Gets the MIME type of the uploaded file, for example: Text/plain. int GetSize (): Gets the size of the uploaded file; InputStream getinputstream (): Gets the input stream corresponding to the uploaded file; void write (file): Upload filesSaved to the specified file. Delete ();  3, file upload to consider a few issues (1) To ensure the security of the server to save the uploaded file directory in the user directly inaccessible places.   (2) Avoid overwriting files so that the file name is unique   (3) Avoid too many files in the same folder scheme one: Break up the storage directory by date Scheme II: Calculate the scattered storage directory with the hashcode of the file name: Level two directory   (4) Limit file Size: Web mode is not suitable for uploading large files single file size: Servletfileupload.setfilesizemax (bytes) Total file size: (multiple file uploads) Servletfileupload.setsizemax ( bytes)   (5) Upload field The user does not upload the problem by judging whether the file name is empty can   (6) The issue of temporary files Diskfileitemfactory: effect: Generate Fileitem object has a cache inside, the cache size by default is 10Kb. If the uploaded file exceeds 10Kb, use disk as the cache. Where is the directory where the cache files are stored? The default is the temporary directory of the system.   If you upload a file by using IO Stream, clean up the temporary file after the stream is closed. Fileitem.delete ();   4, File Download      

21, File upload/download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.