JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Code Review - UploadSave.jspx
This section describes some code logics that resolve some technical issues in UploadSave.jspx.
I am done with all tests on my file upload test application. Now let's have a closer look at my UploadSave.jspx JSP page. It uses a number of logics to resolve some technical issues:
1. How to find out the encryption type? Since encryption type is not submitted in the HTTP request body, I have to use the following code to determine if the encryption type is multipart/form-data:
if (enctype==null) { // try to determine the encryption type if (i>3 && sLine.startsWith("--")) { enctype = "form-data"; boundary = sLine; ...
2. How to process a multiple parts data in a single loop? If multipart/form-data is used, the HTTP request body contains multiple parts, and each part contains a header section and a data section. How to read all the sections and all the parts in a single loop and process them accordingly? I used a status code to indicate the type of the current line in the reading loop. The following code shows you the logic used to determine the status of the current line based the status of the previous line:
if (status.equals("boundary")) { // Expecting the "Content-Disposition:" line status = "disposition"; } else if (status.equals("disposition")) { // Expecting the "Content-Type:" line or a blank line if (sLine.startsWith("Content-Type:")) { status = "type"; } else { status = "blank"; } } else if (status.equals("type")) { // Expecting a blank line status = "blank"; } else if (status.equals("blank")||status.equals("data")) { // Expecting the data or boundary if (sLine.startsWith(boundary)) { status = "boundary"; } else { status = "data"; } }
3. How and when to open and close the files to save the uploaded files? That's easy with the status code calculated correct. See the following the code:
if (status.equals("disposition")) { // Getting the file name and open a file for saving int l = sLine.indexOf("filename="); if (l>=0) { fName = sLine.substring(l+9); fName = fName.replaceAll("\"",""); l = fName.lastIndexOf("\\"); if (l>=0) fName = fName.substring(l+1); if (fName.length()>0) file = new FileOutputStream("\\var\\"+fName); } } else if (status.equals("boundary")) { fName = null; if (file!=null) { file.close(); file = null; } }
4. How to save the uploaded data correctly? Saving the data is easy. But the last line of the data contains an extra new line character. I used a flag "holdNewLine" to hold the new line character if the current line has one:
if (status.equals("data")) { if (file!=null) { if (holdNewLine) file.write(crlf); file.write(line,0,i); holdNewLine = hasNewLine; ...... }
Last update: 2012.
Table of Contents
JSP (JavaServer Pages) Overview
Tomcat 7 Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
Managing HTTP Response Header Lines
Non-ASCII Characters Support in JSP Pages
Overview of JSTL (JSP Standard Tag Libraries)
Multiple Tags Working Together
RFC 1867 - Form-based File Upload in HTML
Code 1 - Display Options - UploadInit.html
Code 2 - Display Form - UploadForm.jspx
Code 3 - Dump File - UploadDump.jspx
Test 2 - POST Method - Successful
Code 4 - Save File - UploadSave.jspx
Test 3 - Save File - Successful