Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Tutorial on FileUpload in Jetty6

In Jetty5, MultiPartRequest was used for file uploading. In Jetty6, MultiPartRequest was changed to MultiPartFilter.

MultiPartFilter decodes the multipart/form-data stream sent by a HTML form that uses a file input item. A File object is then created and stored to the request as an attribute. The attribute name is equal to the name of the file input item of the form being submitted. Therefore if you have a file input item with a name of "userfile1", the attribute name for the File object is "userfile1".

Here is a quick tutorial for uploading files.

First, create the page with a form of enctype equal to multipart/form-data. File uploading won't work if you have a different enctype. Inside the form element, add an input file item.

Code Block
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<title>File Upload Sample</title>
<h1>File Upload Sample</h1>

<form action="fileupload" method="post" enctype="multipart/form-data">

			<td><input type="file" name="userfile1" /></td>
			     <input type="submit" value="Submit" /><input type="reset" value="Reset" />



Create a web.xml with filter and servlet mapping for /fileupload url because that's the action we specified in our form.

Code Block
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""

	<display-name>File Upload Sample</display-name>





Create a FileUpload servlet.

Code Block
package com.exist.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings( "serial" )
public class FileUpload extends HttpServlet
    @SuppressWarnings( "unchecked" )
    protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
        PrintWriter outp = resp.getWriter();

         * if initParam deleteFiles = true you can do either do step 1 or step 2
        // Step 1 --- modify the code inside the for loop in your desired
        ArrayList files = (ArrayList) req.getAttribute( "org.mortbay.servlet.MultiPartFilter.files" );
        for ( int x = 0; x < files.size(); x++ )
            File file1 = (File) files.get( x );
            File outputFile = new File( "outputfile" + ( x + 1 ) );
            file1.renameTo( outputFile );

        StringBuffer buff = new StringBuffer();

        File file1 = (File) req.getAttribute( "userfile1" );

        if( file1 == null || !file1.exists() )
            buff.append( "File does not exist" );
        else if( file1.isDirectory())
            buff.append( "File is a directory" );
            File outputFile = new File( req.getParameter( "userfile1" ) );
            file1.renameTo( outputFile );
            buff.append( "File successfully uploaded." );

        outp.write( "<html>" );
        outp.write( "<head><title>FileUpload page</title></head>" );
        outp.write( "<body>" );
        outp.write( "<h2>" + buff.toString() + "</h2>" );
        outp.write( "</body>" );
        outp.write( "</html>" );

    protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
        doGet( req, resp );


 The FileUpload servlet above simply stores the File being uploaded to the project target directory.

Simply modify the FileUpload servlet to your desired result. Simply bear in mind that the attribute name that you should get, should be the same as the file input item of the form.

Contact the core Jetty developers at
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery