1. Introduce EncType

The encType attribute specifies how form data should be encoded before being sent to the server.

Enctype tells the server the MIME type of the request body (the request header content-type does the same thing).

1, 1 There are three values of encType

value

describe

application/x-www-form-urlencoded

Encodes all characters before sending (default)

multipart/form-data

No character encoding. Each form item is split into a part

text/plain

Spaces are converted to “+” plus signs, but no special character encoding.

1. When the enctype = “application/x – WWW – form – urlencoded ‘

2. When the enctype = “multipart/form – data ‘

Just by looking at this, the body of the request has changed. This request body is called the multi-part request body.

What is a multi-part request body? It splits each form item into a part.

A random string following the content-type boundary of the request header is used as the partition identifier

Common form items:

// Name means the name attribute value 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 uploaded, content-type means the MIME Type, and asdasdas means the content of the file

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

Content-Type: text/plain

asdasdas

3. When the enctype = “text/plain”

The W3C says that Spaces will become “+” + signs, but I don’t see that here, only Spaces will become “+” signs for GET requests

Get into the business

Three conditions must be met to complete the upload

Provide a form. Method must be POST because the transfer data for GET requests is typically 2KB, depending on the browser.

The encType attribute of the form must be multipart/form-data

Provide an upload input field for the input type= “file” class

General implementation principle: When the value of encType is multipart/form-data, the browser will divide each form entry into different parts and use the value of boundary as the segmentation identifier. The character string of this identifier is randomly generated. There will be two more “-” at the end of the segmentation identifier string of the last form entry, representing the end. The server gets the split string with Request. getHeader(” content-type “) and parses it.

Code implementation

I. Construction of development environment

Prepare two third-party JAR packages

Commons – IO package

Commons – upload package

All dependency packages

Code implementation

Prerequisites for downloading

Two heads and one stream

content-type

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

The browser determines how to display the returned message body Content based on the Content-Type.

The default value is text/ HTML

You can use the request. GetServletContext (.) getMimeType (” name “) for the MIME type.

Content-Disposition

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

The default is inline, which opens in a browser window.

When the server sends a file to the client browser, if the file type supported by the browser is generally opened by the browser by default, such as TXT and JPG, which will be directly displayed in the browser.

If you need to prompt the user to save, do something with Content-Disposition, and the key is to always include an attachment.

For example: the Content – Disposition: attachment; Filename = XXX, the browser will activate the download dialog box, attachment represents the attachment, filname is followed by the filename displayed in the download box.

flow

Downloading is responding bytes of data to the client! 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 continuation function, the user can pause in the download process, and continue to download, the server caused less pressure.

Load file list, displayed in the download list

The back-end code logic is mostly the same and currently supports MySQL,Oracle, and SQL. Need to configure the database before using, can refer to me to write this article: blog.ncmem.com/wordpress/2… Welcome to join the group to discuss “374992201”