Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: removed the extra import

...

Code Block
package com.exist.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

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

import
com.exist.util.FileUtils;

@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( "fileUpload1" );

        if( file1 == null || !file1.exists() )
        {
            buff.append( "File does not exist" );
        }
        else if( file1.isDirectory())
        {
            buff.append( "File is a directory" );
        }
        else
        {
            File outputFile = new File( req.getParameter( "fileUpload1" ) );
            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 );
    }

}

...

Contact the core Jetty developers at www.webtide.com
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