ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Uploading Files with Beans
Pages: 1, 2, 3, 4

The whole thing is illustrated by the following lines of code.



while (i != -1 && !newLine.startsWith(boundary)) {
i = in.readLine(line, 0, 128);
if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary)))
pw.print(newLine.substring(0, newLine.length()-2));
else
pw.print(newLine);
newLine = new String(line, 0, i);

}

After the file content is saved to the disk, close the PrintWriter.

pw.close();

The non-file form elements can be retrieved similarly. Instead of writing the values to the disk, however, the name-value pairs are put into the Dictionary object.

fields.put(fieldName, fieldValue.toString());
  • private void setContentType(String s)
    Called from the doUpload method, this method retrieves the uploaded file content type from the raw bytes.
  • private void setFilename(String s)
    Called from the doUpload method, this method retrieved the file path and filename from the raw bytes.

Using the Bean

Once you compile the Bean, you can use it from your Servlet or JSP page. Chances are you are using the Bean with a Servlet/JSP container such as Tomcat. The easiest way to deploy the Bean is to compress the class file into a jar file and chuck it to the lib directory of Tomcat. You need to restart Tomcat for the jar file to be loaded.

The following are an HTML file and a JSP file that illustrate the use of the Bean. The HTML file contains a form with a few elements.

<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action=jsp1.jsp enctype="MULTIPART/FORM-DATA" method=post>
Author: <input type=text name=author>
<br>
Company: <input type=text name=company>
<br>
Comment: <textarea name=comment></textarea>
<br>
Select file to upload <input type=file name=filename>
<br>
Description: <input type=text name=description>
<br>
<input type=submit value="Upload">
</form>
</body>
</html>

When the user submits the form, the HTTP request will be handed to Jsp1.jsp, which uses the FileUpload Bean to process the request. The code of the Jsp1.jsp is given below.

<jsp:useBean id="TheBean" scope="page"
class="com.brainysoftware.web.FileUploadBean" />
<%
TheBean.doUpload(request);
out.println("Filename:" + TheBean.getFilename());
out.println("<BR>Content Type:" + TheBean.getContentType());
out.println("<BR>Author:" + TheBean.getFieldValue("Author"));
out.println("<BR>Company:" + TheBean.getFieldValue("Company"));
out.println("<BR>Comment:" + TheBean.getFieldValue("Comment"));
%>

Summary

File upload is not an easy topic and code to process it is not readily available. This article has presented the theory as well as the code for a File Upload Bean. The compiled version of the Bean is available for download from my Web site.

Budi Kurniawan is a senior J2EE architect and author.


Return to ONJava.com.