当前位置:网站首页>Project Notes - Take Notes

Project Notes - Take Notes

2022-08-11 06:57:00 kerodaisuki

Three elements of file upload

1. Form submission method: POST (there is a size limit for get method submission, post does not)

2. The enctype attribute of the form: must be set to multipart/form-data

Enctype means encodetype means encoding type

Multipart/form-data is a multi-part file upload, which means that the form data is composed of multiple parts, which means both text data and binary data such as files.

3. The form must have a file upload item: file, which must have a name attribute and value

Note: By default, the value of the enctype of the form is application/x-www-form-urlencoded, which cannot be used for file uploading. Only when multipart/form-data is used, the file data can be completely transmitted

Files uploaded by the server

1. Get the content of the request body through request

2. Parse the request body. The feature of multi-component upload is that each input is a form item.

Cut all the content in the request into an array according to the delimiter, each element in the array is a form item

3. Traverse the array and judge which is a normal form and which is a file upload item according to the filename

4. Get the content of the common form item and get it through the attribute name

5. Get the content of the file upload item

File name

File content

6. Use IO to save the file content to the server

FileUpload tool class

1. Import dependencies

The FileUpload package makes it easy to upload files to web applications

IOUtils encapsulates common operations in java and is very convenient to use

2. Introduction to FileUpload core class

DiskFileItemFactory: Disk file item factory, related configuration when reading files, such as: cache size, temporary directory location

ServletFileUpload:A core class for file uploading

FileItem: Represents each form item

3. Detailed explanation of file upload API

ServletFileUpload

isMultipartContent(request); : determine whether it is a file upload form

parseRequest(request); : parse the request to get a collection of form items

setHeaderEncoding("utf-8"): Set the encoding format of the uploaded filename

FileItem

isFormField(): Determine whether it is a normal form item

getFiledName(): Get the name property of the form

item.getString(): Get the value of the form

getName() : Get the name of the uploaded file

getInputStream():Get the uploaded file

delete(): deletes temporary files

4. File upload background code writing

FileUpload steps:

1. Create a disk file item factory

2. Create a core class for file uploading

3. Parse the request - get the file item collection

4. Traverse the file item collection

5. Judging common form items/file upload items

原网站

版权声明
本文为[kerodaisuki]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110516481702.html