JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

File Upload

Part:   1  2  3  4  5 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

This chapter shows you:

  • What are the requirements to upload files to Web servers.
  • The RFC 1867 extension of HTML for uploading files.
  • How to write a HTML page to upload files, and dump them back to the browser.
  • How to read uploaded files out of the HTTP request, and save them on the Web server.

Requirements for File Upload

To set up a Web page for users to upload files, you need to:

  • Understand how file upload works - read the file upload specification.
  • A Web page with special HTML tags to instruct the browser to include a file in the HTTP request.
  • A Web server handler program to handle the HTTP request that contains the uploaded file.

RFC 1867 - Form-based File Upload in HTML

In order to upload files to Web servers, RFC 1867 - Form-based File Upload in HTML was proposed by E. Nebel and L. Masinter in 1995. It offers:

1. A new type code, FILE, for the INPUT HTML tag:

   <INPUT TYPE="FILE" NAME="field_name" VALUE="file_name">

A Web browser should interpret this tag as an input field to enter a file name on the local system. Some browsers, like Internet Explorer, will also display button called "Browse" next to this field to allow users to browse the local file system to select a file name.

2. A new encryption type code, multipart/form-date, for the FORM HTML tag:

   <FORM ACTION="url" METHOD="post" ENCTYPE="multipart/form-data">

When a user clicks the submit button on Web form with this tag, the Web browser should collect data from all input fields and submit to the specified URL. The browser should also follow the following rules:

  • If "get" is specified as the method, data will be submitted as part of the GET header line of the HTTP request.
  • If "post" is specified as the method, data will be submitted as the body of the HTTP request.
  • If "application/x-www-form-urlencoded" is specified as the encryption type, names and values of all input fields will be encoded together based on the URL encoding specification.
  • If no encryption type is specified, "application/x-www-form-urlencoded" will be used as the default encryption type.
  • If "multipart/form-data" is specified as the encryption type, data will be submitted in multiple parts with one part for one input field. Parts are separated by a boundary identification string, which should be given as the "boundary" attribute of the "Content-type" header line if the HTTP request.
  • If "application/x-www-form-urlencoded" is specified as the encryption type, only the file name of a FILE type input field will be submitted as the value of the field. The content of the file will not be submitted.
  • If "multipart/form-data" is specified as the encryption type, only the file name of a FILE type input field will be submitted as the value of the field. The content of the file will not be submitted.

When "multipart/form-data" is specified as the encryption type, the format of the HTTP request body should look like this:

--boundary_identification_string
input_part_1
--boundary_identification_string
input_part_2
--boundary_identification_string
input_part_3
......
--boundary_identification_string--

The format of each input part is depending on the input field type. For any types other than FILE, the format should look like this:

Content-Disposition: form-data; name="field_name"

field_value

For an input field of type FILE, the format of the input part should look like this:

Content-Disposition: form-data; name="field_name"; filename="file_name"
Content-Type: file_type

file_content

(Continued on next part...)

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - File Upload