File Upload Processing

Source: Internet
Author: User

I. Front-Office processing

1. Using the form INPUT element

<formenctype= "Multipart/form-data"Method= "POST"Action= "http://123.jsp">    <inputtype= "File"name= "File"ID= "File"multiple/><BR/></form>

Here, pay special attention to the Enctype property of the form, a total of three kinds

1) application/x-www-form-urlencoded (default value)

, which is the default form submission method.

  

This request only handles the Value property of the element in the form, and the Value property is URL-encoded before it is sent to the server, and the contents of the file are not transferred.

, the HTTP request only sends the name and value of the input element (that is, the selected file name)

  

When looking at the original HTTP request header, it is true that the Chinese file name has been URL-encoded, and the background can get the value through GetParameter because the Web server has processed the original data once.

  

  If we use the default Enctype property value, we cannot implement a file upload.

2) Multipart/form-data

The earliest HTTP post was not supported for file uploads, and in 1995 the IETF introduced rfc1867 (RFC 1867-form-based file Upload in HTML) to support file uploads. That is the Multipart/form-data way.

Multipart/form-data is made up of three parts

The first part is the first line, which represents the boundary (boundary)
The second section represents some content configuration information for the file, typically form-data (because it was submitted in Formdata object format), followed by other name=value information.
The third part is the third line, transfer the contents of the file.

Specific as shown.

  

The boundary of the first line is randomly generated by the browser, can be obtained in the request header Content-type, the background program divides the multiple files according to this boundary, and then parses them one by one.

  

When set to this value, the background will no longer be able to get the parameter value through GetParameter. If you want to pass some parameters at the same time, you can add "?" after the URL. Name=value ".

3) Text/plain

Text/plain means plain text transmission, and the browser does not encode. This method is generally not used in the submission form.

2. Form optimization

Ordinary upload file form submitted, will jump, if the file is large also need to wait for the server response before the upload results, this way the user experience is not good.

We want to send the request after the page does not jump, the user can continue to operate the page, you can also observe the upload progress waiting results. You can therefore use the new Formdata object in HTML5 and XMLHttpRequest2.0 to implement this functionality.

Using the Formdata object, we can use a series of key-value pairs to simulate a complete form, and then use the XMLHttpRequest Send () method to submit the "form" asynchronously. Formdata with Ajax implementation of the progress bar asynchronous file upload, you can refer to the MDN

Https://developer.mozilla.org/zh-CN/docs/Web/Guide/Using_FormData_Objects

Https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications

Two. Background processing

1. parsing files

The data can be parsed using multipart/form-data to the background, and in theory it can be parsed by Request.getinputstream () to get the request text, but this is a complicated task and you have to be familiar with the RFC standards. There are already open source projects in Java that help us parse files uploaded using Multipart/form-data.

Download Commons-fileupload, Commons-fileupload-io two jar packages on Apache website and import them into the project Lib directory, you can use them to parse the files.

Note that the body of the HTTP request has only one read/parse chance, and if the following function is called before parsing with fileupload, fileupload resolves the failure.

request.getParameter();

request.getParameterMap();

request.getParameterNames();

request.getParameterValues();

request.getReader();

request.getInputStream();

If it is servlet3.0 (TOMCAT7) version, you can use Java Native API Httpservletrequest.getpart ()

Specific reference: http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824

2. File storage

After parsing the contents of the file in the background, you can store the contents of the file. We can use Request.getservletcontext (). Getrealpath ("/web-inf/filename") to get the path to the file stored in the WEB directory, The content is written to the disk file via FileOutputStream. But there is a flaw in this approach.

1). Files are uploaded to the Web Deployment directory, and once the project is redeployed, all uploaded files are lost, so generally uploaded files are saved in the database.

2). If the Web container is not configured to expand the war file to a physical disk, but instead is deployed as a virtual file system in memory, Getrealpath () will have an error, and it may return null or the wrong path

3). You should try to avoid using the Getrealpath () method. You need to get the input stream from the server to use

ServletContext.getResourceAsStream() 

ServletContext.getResourcePaths()

ServletContext.getResource() (必须以"/"开头)

等方法替代。

  

File Upload Processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.