4.6. Retrieving Values of Ordinary Form Fields with Java Servlet / JSP

If a file item contains a simple name-value pair of an ordinary form field, we can retrieve its name and value using the getFieldName() method and the getString() method respectively:


String name = fileItem.getFieldName();
String value = fileItem.getString();


For example, suppose there is a text field in an HTML/XHTML form:


<input name="text_field" type="text" />


and you enter "Welcome to our JSP / Servlet file upload tutorial" in the text field. After the execution of the previous two lines of Java code, the name variable should contain the value "text_field" (the name attribute value of the <input> tag) and the value variable should contain the value "Welcome to our JSP / Servlet file upload tutorial".


4.7. Getting Information about the Uploaded File with Java Servlet / JSP

If a file item contains an uploaded file, we can use a number of methods to obtain some information about the uploaded file before we decide what to do with it:


/* Get the name attribute value of the <input type="file"> element. */
String fieldName = fileItem.getFieldName();

/* Get the size of the uploaded file in bytes. */
long fileSize = fileItem.getSize();

/* Get the name of the uploaded file at the client-side. Some browsers such as IE 6 include the whole path here (e.g. e:\files\myFile.txt), so you may need to extract the file name from the path. This information is provided by the client browser, which means you should be cautious since it may be a wrong value provided by a malicious user. */
String fileName = fileItem.getName();

/* Get the content type (MIME type) of the uploaded file. This information is provided by the client browser, which means you should be cautious since it may be a wrong value provided by a malicious user. */
String contentType = fileItem.getContentType();


Nokia cell phones such as Nokia 6230 determine the content type (MIME type) of the file to be uploaded by its file extension. The following table lists some of the file extensions that are recognized by Nokia 6230. We have shown this table to you before and we just copy and paste it here for your convenience.


File extension

Content type / MIME type

.jpg

image/jpeg

.gif

image/gif

.png

image/png

.wbmp

image/vnd.wap.wbmp

.txt

text/plain


If the Nokia 6230 cell phone does not recognize a file extension, it will specify "application/octet-stream" as the content type / MIME type of the file in the HTTP request.


4.8. Saving Uploaded Files in the File System with Java Servlet / JSP

In some situations, you just want to store the uploaded file in the file system without concerning what the uploaded file contains. The FileItem interface provides a method called write() that helps us perform this easily:


File saveTo = new File("/upload_files/myFile.txt");
fileItem.write(saveTo);


(In the above Java code snippet, File is a class of the java.io package.)

If everything works fine, the uploaded file will be saved to "/upload_files/myFile.txt". Otherwise the write() method will throw a java.lang.Exception exception.

Note that if the write() method is called more than once, an error may occur. In the case where the uploaded file is stored as a temporary file on the disk, the write() method will first try to rename the temporary file and put it at the new location instead of copying the file contents so as to obtain a performance gain. When the write() method is called the second time, the temporary file does not exist and so it will produce an error. However, if the uploaded file is hold in the memory, you can call the write() method multiple times.


4.9. Processing Contents of Uploaded Files with Java Servlet / JSP

If you do not want to save the uploaded file directly but to process it, the get() and getInputStream() methods can help you. The get() method returns the uploaded file as an array of the byte data type:


byte[] fileData = fileItem.get();


However, if the uploaded file is large in size, you will not want to load the whole file into memory. The getInputStream() method can help you in this case. It returns the uploaded file as a stream:


InputStream fileStream = fileItem.getInputStream();


(InputStream is a class of the java.io package.)


Previous Page Page 9 of 11 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page