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

(Continued from previous part...)

Here is sample HTML page to show you how to use FILE input field and "multipart/form-data" encryption type:

<?xml version="1.0"?>
<html><body>
<!-- UploadSample.html
     Copyright (c) 2004 by Dr. Herong Yang
-->
<form action="some_url" enctype="multipart/form-data" method="post">
<input type="text" name="author" value="Herong Yang"/>
<input type="file" name="picture" value="C:\dot.gif"/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body></html>

The browser should submit the following multiple-part HTTP request body when users click the submit button:

--boundary_identification_string
Content-Disposition: form-data; name="author"

Herong Yang
--boundary_identification_string
Content-Disposition: form-data; name="picture"; filename="C:\dot.gif"
Content-Type: image/gif

dot.gif_file_content
--boundary_identification_string
Content-Disposition: form-data; name="submit"

Submit
--boundary_identification_string--

UploadDump.jsp - Dumping Uploaded Files

Now we know how to write a HTML page for uploading files. What's next is to know how to handle the uploaded files embedded in the HTTP request on the Web server side.

Unfortunately, normal Web servers do not offer any support to handle the uploaded files. JSP also does not offer any support to handle the uploaded files automatically. We need additional special programs on the Web server side to do this job.

Of course, there are a lot existing programs available on the Internet that can be installed on the Web server to handle uploaded files. But writing such a program yourself is also not very hard. Here is my first program, UploadDump.jsp, which will read the HTTP request body, and dump the body back to the browser:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!--
 - UploadDump.jsp
 - Copyright (c) 2004 by Dr. Herong Yang. All rights reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>
<jsp:scriptlet><![CDATA[
   out.println("<pre>");
   ServletInputStream in = request.getInputStream();
   byte[] line = new byte[128];
   int i = in.readLine(line, 0, 128);
   while (i != -1) {
      out.print(new String(line, 0, i));
      i = in.readLine(line, 0, 128);
   }
   out.println("</pre>");
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>

As you can see, this JSP program is very simple. The key is the request.getInputStream() call, which returns a ServletInputStreem object connecting to the HTTP request body. Reading the data from the stream object and dump it back to the browser is easy.

To test this program, and to see how my browser is submitting different types of input fields with different encryption types, I wrote a simple HTML file, UploadInit.html, which will let you change the upload settings:

<?xml version="1.0"?>
<html><body>
<!-- UploadInit.html
     Copyright (c) 2004 by Dr. Herong Yang
-->
<b>File Upload Test Settings:</b><br/>
<form action="UploadForm.jsp" method="get">
File upload handler:<select name="action">
<option>UploadDump.jsp</option>
<option>UploadSave.jsp</option>
</select><br/>
Submit method:<select name="method">
<option>get</option>
<option>post</option>
</select><br/>
Encryption type:<select name="enctype">
<option value="x-www-form-urlencoded">
application/x-www-form-urlencoded</option>
<option value="form-data">multipart/form-data</option>
</select><br/>
<input type="submit" name="submit" value="Submit"><br/>
</form>
</body></html>

(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