Home> Java> javaTutorial> body text

Detailed explanation of java_file upload example

怪我咯
Release: 2017-06-25 10:04:02
Original
1344 people have browsed it

Today’s content:

File upload------commons-fileupload

The essence of file upload and download: copy of file

File upload: copy from local to server disk The client needs to write a file upload form---- >The server needs to write code to accept uploaded files

File download: copy from the server disk to the local disk You need to write server-side code, and the client's download work is solved by the browser kernel

##1. File upload

1, File upload steps:

(1) Need to write a file upload form

(2) Write server-side code with For receiving uploaded files

2, client encoding

Notes:

3, the server accepts the form for file upload Data

Principle of file upload and reception

Easy and quick to use File upload tool---Apache Commons-fileupload.jar

Usage steps:

(1) Download the jar package for file upload fileuplaod with the help of commons-io.jar

(2) Use the API in the fileupload tool Encoding

4, the three core objects in the FileUpload tool

DiskFileItemFactory: Disk file item factory--some related configuration settings The size of the cache and the location of the temporary directory

ServletFileUplaod: A core class for file upload

FileItem: Represents each form item

5. Quick Start

6, Detailed explanation of file upload API

(1) DiskFileItemFactory

Set the cache size: factory.setSizeThreshold() in bytes

Set the directory of temporary files: factory.setRepository(File)

(2) ServletFileUpload

Determine whether it is a file upload form: ServletFileUpload.isMultipartContent(request);

Parse request to obtain a collection of form items: upload.parseRequest(request);

Set the encoding method of the uploaded file name: upload.setHeaderEncoding("UTF- 8");

(3) FileItem

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

Get the name attribute value of the form: item.getFieldName();

Get the value value of the form: item.getString("UTF-8") --- UTF-8 represents the encoding when obtaining Chinese

Get the name of the uploaded file: item.getName()

Get uploaded files: item.getInputStream();

Delete temporary files: item.delete();

The above is the detailed content of Detailed explanation of java_file upload example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!