Javaweb upload and download of the implementation file

Source: Internet
Author: User

1. What is upload and download?

-Data upload means that the client uploads data to the server, and all requests sent by the client to the server belong to the data upload. File upload is a special case of uploading data, which means that the client uploads files to the server. The file to be saved on the client is uploaded to a copy of the server and saved to the server.

-Data download refers to the process by which the client obtains data from the server. File download is a special case of data download, refers to the client to download files from the server, the original saved in the server file download to a copy of the client to save. Most of the requests that we make to the server, most of which are file download requests, download text, pictures, sounds, videos, and so on from the server, and then parse the files by the client browser before you can see the multimedia information.

-but what we are talking about here is the file download, which refers to the file downloaded from the server to the browser, the browser does not directly parse, but as an attachment to the form of saving to the client.

-Upload and download files can be text files, pictures, sounds, videos and other types.

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/29/b8f92803d83bf1eeb80b10fbce113a91.png "title=" 00.png "alt=" B8f92803d83bf1eeb80b10fbce113a91.png "/>

2, the implementation of file upload:

A, the requirements of the form when uploading:

-File upload requires a client form to submit a special request--multipart request, that is, a request that contains multipart data. Therefore, the file upload form is required for the encoding type of the form data and must be multipart/form-data. That is, to specify the Enctype property value of "Multipart/form-data" for the <form> tag. (Enctype, that is, Encodingtype, the encoding type.) )

-Because the size of the client upload file is indeterminate, the HTTP protocol stipulates that the data uploaded by the file is stored in the request body and cannot appear in the URL's address bar because the amount of data that can be stored in the address bar is too small. That is, the file uploads the form and must submit a POST request without being able to submit a GET request.

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/23a26a65cfe7c6b1dc5580e44b9df7dc.png "title=" 01.png "alt=" 23a26a65cfe7c6b1dc5580e44b9df7dc.png "/>


B, using third-party tools to achieve file upload:

-Need to use up to two jar packages: Commons-fileupload-1.3.3.jar and Commons-io-2.6.jar,jar packages can be downloaded from the Apache website (Commons-fileupload-1.3.3.jar relies on commons- Io-2.6.jar, if only the FileUpload jar package is used, it will throw an exception when running the program)

-The sample code is as follows:

-The code that is filled out in the index.jsp file is as follows:

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/29/2fa75419bc90658bfde414ccaace60b4.png "title=" 03.png "alt=" 2fa75419bc90658bfde414ccaace60b4.png "/>


-Add a servlet to the Web app to add configuration information to the XML. config file:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/f48d3786832f863a3a9040d67d06d6c3.png "style=" float: none; "title=" 01.png "alt=" F48d3786832f863a3a9040d67d06d6c3.png "/>


-Customize the Serlvet class to write a simple implementation of the file upload code in the Dopost method:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/bf00d1a00b4fb45347508f08e3538a8f.png "title=" 02.png "alt=" Bf00d1a00b4fb45347508f08e3538a8f.png "/>


-Start the web app, Access index.jsp, add pictures, click "Upload", you can find the console printing results, in the workspace Web project in the specified directory can see the picture has been uploaded: (in Eclipse, The default is to put the Web project under Eclipse's workspace. Metadata\.plugins\org.eclipse.wst.server.core\tmp0 (or TMP1) \wtpwebapps\)

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/29/b21e32099f0c94dbd9f2c5ce326a994f.png "style=" float: none; "title=" 05.png "alt=" B21e32099f0c94dbd9f2c5ce326a994f.png "/>

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/29/729ee6601f720fb6fd4c181782ac64b3.png "title=" 06.png "alt=" 729ee6601f720fb6fd4c181782ac64b3.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/29/314014bfe306dd1b3104d4a0c3cccff5.png "style=" float: none; "title=" 07.png "alt=" 314014bfe306dd1b3104d4a0c3cccff5.png "/>


-code-related explanations in the Fileuploadservlet class: (also includes common methods for uploading with FileUpload jar package files)

-Org.apache.commons.fileupload.disk.DiskFileItemFactory class: The default Fileitemfactory implementation class. This implementation creates a Fileitem instance (file entry). If it is a small entry, it will be stored in memory, and if it is a large entry, it will be stored in a temporary file on the hard disk. The critical value of the size entry data is configurable.

650) this.width=650; "src=" Https://s5.51cto.com/oss/201710/29/91f1190a16a81a93e97408f2bd99b76b.png "title=" 04.png "alt=" 91f1190a16a81a93e97408f2bd99b76b.png "/>

-Setrepository (File repository) method: Sets the directory location where temporary storage files (this file size is larger than the specified configuration threshold) are stored.

-Setsizethreshold (int sizethreshold) method: Sets the critical value (in bytes), and when this value is exceeded, the file is written directly to the hard disk. (If Sizethreshold is 1024 * 1024, then 1 MB)

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/8a74cc4e8caa684bf269b300c189b40d.png "title=" 00.png "alt=" 8a74cc4e8caa684bf269b300c189b40d.png "/>



-Org.apache.commons.fileupload.servlet.ServletFileUpload class: Advanced API for handling file uploads. (How the data for each part is stored is determined by the factory that created them; the given part may be in memory, on disk, or elsewhere)

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/29/9c3076d9c62c9f9776b5195657472c23.png "title=" 08.png "alt=" 9c3076d9c62c9f9776b5195657472c23.png "/>

-Parserequest Method: Parse the request to get the list object of the Fileitem type element:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/29/ff0d0aefa0fbc00c740f398a0a637473.png "title=" 09.png "alt=" Ff0d0aefa0fbc00c740f398a0a637473.png "/>


-Method of the parent class of the Servletfileupload class Org.apache.commons.fileupload.FileUploadBase:

-Set the header character encoding for each item, which resolves the Chinese garbled problem with the file name: (if the Tomcat version is lower, the file name may be garbled)

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/29/b3a6b7eea773e4b8ebd511b28023c444.png "title=" 02.png "alt=" B3a6b7eea773e4b8ebd511b28023c444.png "/>

-The Setfilesizemax (long Filesizemax) method sets the maximum boundary value that a single upload file allows to upload.

-The Setsizemax (long Sizemax) method sets the maximum boundary value for the sum of all files uploaded at one time. (Units are bytes)

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/29/f901e54502e4278348b152c03449577e.png "style=" float: none; "title=" 04.png "alt=" F901e54502e4278348b152c03449577e.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/8017fa8425940edd962e9e9a10e20d1a.png "style=" float: none; "title=" 05.png "alt=" 8017fa8425940edd962e9e9a10e20d1a.png "/>



-Ismultipartcontent (HttpServletRequest Request) Method: Determine if the request contains multipart content

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/29/8618a209ca6c1659043e646ea7cccc40.png "title=" 10.png "alt=" 8618a209ca6c1659043e646ea7cccc40.png "/>


-Org.apache.commons.fileupload.FileItem Interface: Represents a file or form entry within a Multipart/form-data POST request.

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/29/1f9e35cac9e80e53f4a390644a76cfec.png "title=" 11.png "alt=" 1f9e35cac9e80e53f4a390644a76cfec.png "/>

-Isformfield method: Determine if the Fileitem instance is a normal form item:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/5eefe2a855a7948e14135b4a880c8e03.png "title=" 12.png "alt=" 5eefe2a855a7948e14135b4a880c8e03.png "/>

-GetFieldName method: Gets the Name property value for the corresponding file entry (that is, a single form item):

650) this.width=650; "src=" Https://s4.51cto.com/oss/201710/29/be8a739ea055325c48ae810618218c29.png "title=" 13.png "alt=" Be8a739ea055325c48ae810618218c29.png "/>

-GetName method: Gets the original upload file name in the client file system: (contains the suffix name)

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/795af79e3b6ec7c2e381e0d6d9a9031a.png "title=" 14.png "alt=" 795af79e3b6ec7c2e381e0d6d9a9031a.png "/>

-getInputStream method: Gets the input stream that is used to remove the contents of the uploaded file:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/8de79b5a5c3a09749d73d093dd334d49.png "title=" 15.png "alt=" 8de79b5a5c3a09749d73d093dd334d49.png "/>

-Delete method: Delete any associated temporary disk files:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/29/e12b9cb5850a53fbffb4cd2f2bf68a89.png "title=" 01.png "alt=" E12b9cb5850a53fbffb4cd2f2bf68a89.png "/>


3, the implementation of file download:

A, Hyperlink download: The download resource as a hyperlink link destination file appears. If the browser can parse the resource file, the contents of the file will be displayed directly on the browser, and if the browser does not support parsing of the file, the Save As dialog box will pop up and ask the user to save it.

-The disadvantage of this download is obvious, different browsers, and the same browser installed by different plug-ins, then its ability to resolve the resource is different, whether it pop-up as a dialog box is not the same situation. The right to decide is in the browser's hands.

-If you add the following code to the JSP file, click in the FF browser to open the picture directly in the browser:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/30/e4d08a49cb50400b656faf31c560867c.png "title=" 00.png "alt=" E4d08a49cb50400b656faf31c560867c.png "/>


b, the servlet mode file download:

-Sample code:

-The contents of the JSP file and the configuration added in the Web. xml file:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/30/695609a242b68865df8a6ea3016f6786.png "style=" float: none; "title=" 04.png "alt=" 695609a242b68865df8a6ea3016f6786.png "/>

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/30/d3d6402faa2673bb661ffa7358127a35.png "style=" float: none; "title=" 03.png "alt=" D3d6402faa2673bb661ffa7358127a35.png "/>


-Add the following code to the custom servlet class:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/30/46f022c912973e64c35f98421c07ec76.png "title=" 01.png "alt=" 46f022c912973e64c35f98421c07ec76.png "/>

-If you want the browser to be downloaded as an attachment, you need to modify the response's Header property content-disposition value to attachment;filename= file name + suffix name. (filename indicates that the displayed file name is downloaded as an attachment)

-

Javax.servlet. getResourceAsStream method for the ServletContext interface: Gets the resource under the specified path in the Web app, returned as a byte input stream inputstream.

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/30/9974aebf2e1671c4a312cfe30c016d58.png "title=" 02.png "alt=" 9974aebf2e1671c4a312cfe30c016d58.png "/>


-Start the Web app and click the hyperlink to see the file download as an attachment:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201710/30/e4f78ad2d6a233bc7e5460214d89d0b8.png "title=" 05.png "alt=" E4f78ad2d6a233bc7e5460214d89d0b8.png "/>


4. Additional Instructions:

-Upload the file directory management: When the user uploads too many files, and a single directory can hold a limited number of files, (Windows system in a single folder up to 65,534 files) so the need to upload files sometimes need to be managed in separate directories.

-You can use the mkdir () method of the Java.io.File class to create a direct subdirectory in the current directory, or use the Mkdirs () method to create a multilevel subdirectory of the current directory:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201710/29/de46eac949cf2dbf440d624822609a0f.png "title=" 06.png "alt=" De46eac949cf2dbf440d624822609a0f.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/oss/201710/29/cc09c6e9d0f6817a27352d4e29589681.png "title=" 08.png "alt=" Cc09c6e9d0f6817a27352d4e29589681.png "/>


This article is from the "12392717" blog, please be sure to keep this source http://12402717.blog.51cto.com/12392717/1977293

Javaweb implementation file upload and 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.