java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4096
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Uploading file using JSP E-mail
User Rating: / 339
PoorBest 

It is often necessary to upload some file from the client machine to server. There is a special input type file in HTML forms to allow the visitor browser local file system to select the file. When the file is selected it is sent to server as a part of a POST request. During this there are two mandatory restrictions applied to the form with input type file: it must contains attribute enctype set to value multipart/form-data and its method should be POST. Thus, entire request in sent to server in encoded form. JSP container does not parse the content od requests with type multipart. That is why JSP or servlet processing incoming file data has to use own means to handle request and extract a file from there.

There are a lot of free implementations of a multipart-request decoders on the web. We use here utility class javazoom.upload.MultipartFormDataRequest dowloaded within UploadBean from http://www.javazoom.net.

A form with file input for uploaded file selection:

..........
<form action="upload.jsp"
  method="post" enctype="multipart/form-data">

  Select a file: 
  <input type="file" name="first" />
  
  <br />
  <input type="submit" name="button" value="upload" />
  
</form>
..........

The page processing request with file encoded:

<%@page contentType="text/html;"%>

<%@page import="java.util.Hashtable"%>
<%@page import="javazoom.upload.MultipartFormDataRequest" %>

...........

<%
  try {
    // decode source request:
    MultipartFormDataRequest data = 
       new MultipartFormDataRequest(request);

    // get the files uploaded:
    Hashtable files = data.getFiles();
    
    if (! files.isEmpty()) {
      // do something with collection of files uploaded;
      ........
    else {
      throw new IllegalStateException("No files supplied");
    }
  catch (RuntimeException error) {

    // set error flag in session:
    request.getSession().setAttribute("error", error);

    // throw its further to print in error-page:
    throw error;
  }
%>
...........


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.