Implementation technology of uploading and downloading JAVA large files (above 1G)

1. Introduction to enctype

The enctype attribute specifies how the form data should be encoded before sending to the server.

The function of enctype is to inform the server of the MIME type of the request body (the function of the request message header content-type is the same)

1. There are three values ​​for enctype

value

description

application/x-www-form-urlencoded

Encode all characters before sending (default)

multipart/form-data

The characters are not encoded. Each form item is divided into a part

text/plain

Spaces are converted to "+" plus signs, but special characters are not encoded.

1. 当enctype=’application/x-www-form-urlencoded’

2.当enctype=’multipart/form-data’

 

Observation shows that the request body has changed. This request body is called a multi-part request body.

What is a multi-part request body: it divides each form item into a part.

Take a string of random strings after the boundary of the content-type of the request header as the segmentation identifier

Common form items:

//name means the attribute value of name in the text box, and admin is the text value we entered

Content-Disposition: form-data; name="username"

admin

File form item

//filename means: the name of the file we upload, content-Type means: MIME type, asdasdas means: the content in the file

Content-Disposition: form-data; name="upload"; filename="a.txt"

Content-Type: text/plain

asdasdas

3. 当enctype=’text/plain’

w3c said: the space will become a "+" plus sign, but I did not find it here, only when the get request, the space will become a "+" sign

Into the title

3 necessary conditions must be met to complete the upload

To provide form, the method must be post, because the transmission data of get request is generally 2kb, which is different for different browsers.

The form attribute enctype must be multipart/form-data

Provide upload input field of input type=”file”

Rough realization principle: When the value of enctype is multipart/form-data, the browser will divide each form item into different parts, and use the value of boundary to divide the identifier. The string of this identifier is randomly generated , There will be two more "- -" at the end of the split identification string of the last form item, representing the end. The server uses request.getHeader("content-type") to obtain the segmented string, and then parses it.

 

Code

1. Development environment construction

Prepare two third-party jar packages

commons-io package

commons-upload package

All dependent packages

Code

Requirements for download

Two first one flow

content-type

Content-Type is a very important content in the returned message, indicating what MIME type the document content belongs to.

The browser will determine how to display the returned message body content based on the Content-Type.

The default value is text/html

You can use request.getServletContext().getMimeType("file name") to get the MIME type.

Content-Disposition

Content-disposition is an extension of the MIME protocol, which instructs the MIME user agent how to display the attached files.

The default value is inline, which means to open in a browser window.

When the server sends a file to the client browser, if it is a file type supported by the browser, it will generally be opened by the browser by default, such as txt, jpg, etc., which will be displayed directly in the browser.

If you need to prompt the user to save, use Content-Disposition for some processing, the key is to add attachment.

For example: Content-Disposition:attachment;filename=xxx, the browser will activate the download box dialog box, attachment means attachment, and filname followed by the file name displayed in the download box.

flow

Download is to respond to the client with byte data! To turn a file into a byte array, use response.getOutputStream()

To respond to the browser.

The code is as follows, this code has realized the breakpoint resume function, the user can pause and continue the download during the download process, and the pressure on the server is relatively small.

 

Load the file list and display it in the download list

Most of the back-end code logic is the same, and it currently supports MySQL, Oracle, and SQL. You need to configure the database before use, you can refer to this article I wrote: http://blog.ncmem.com/wordpress/2019/08/12/java-http%E5%A4%A7%E6%96%87 %E4%BB%B6%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0%E4%B8%8A%E4%BC%A0/ 
Welcome to join the group to discuss "374992201 "

Guess you like

Origin blog.csdn.net/weixin_45525177/article/details/108472073