1. Introducing enctype
The enctype attribute specifies how the form data should be encoded before being sent to the server.
The role of enctype is to inform the server of the MIME type of the request body (the content-type of the request header is the same)
1. There are three values for 1 enctype
value |
description |
application/x-www-form-urlencoded |
Encode all characters before sending (default) |
multipart/form-data |
Does not encode characters. 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’
Observed that the request body has changed. This request body is called a multi-component request body.
What is a multi-part request body: is to divide each form item into a part.
Use a random string after the content-type boundary 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 uploaded, content-Type means: MIME type, asdasdas means: the contents of 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
To complete the upload, 3 necessary conditions must be met
To provide the form form, the method must be a post, because the transfer data of the get request is generally 2kb, which is different for different browsers.
The form attribute enctype must be multipart / form-data
Provide input input field with input type = ”file”
Rough implementation principle: when the value of enctype is multipart / form-data, the browser will divide each form item into different parts, and divide the identifier with the value of boundary, the string of this identifier is randomly generated , The end of the last form item's split identification string will be two more "--", which means the end. The server uses request.getHeader ("content-type") to get the split string and then parse it.
Code
1. Development environment
Prepare two third-party jar packages
All dependent packages
Code
Requirements for download
Two heads and one stream
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 decide how to display the content of the returned message body according to 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 attached files.
The default value is inline, which means it opens 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.
If you need to prompt the user to save, use Content-Disposition for processing, the key is to add attachment.
For example: Content-Disposition: attachment; filename = xxx, the browser will activate the download box dialog, attachment means attachment, and filname is followed by the file name displayed in the download box.
flow
Downloading is to respond to the client's 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 implemented the breakpoint resume function. The user can pause and continue the download during the download process, which causes less pressure on the server.
Load the file list and display it in the download list
The back-end code logic is mostly the same, and currently supports MySQL, Oracle, SQL. Before using, you need to configure the database, 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 "