0

I am trying to allow users to upload a file using a HTML form. The form input will be processed by my Java Servlet doPost method.

I am currently getting a NPE as Java is unable to read back the file from the POST request.

The request.getPart( "file" ) is returning null instead of the file. Can anyone explain where I've gone wrong here?

Thanks

HTML Form

<form method="post" action="Serv/FileUpload" id="myForm" enctype="multipart/form-data" >
<input multiple="true" type="file" name="file" label="Select Some Files" id="file" />
<input type="submit" label="Submit" value="Upload attached file"/>

Servlet Method

private void fileUpload( HttpServletRequest request, HttpServletResponse response, Connection connection, PrintWriter out ) throws ServletException,
                                                                                                                           IOException
{

    response.setContentType( "text/html;charset=UTF-8" );

    final String path = "uploads/";
    final Part filePart = request.getPart( "file" );

    System.out.println( "File part " + filePart );

    final String fileName = getFileName( filePart );

    OutputStream outStream = null;
    InputStream filecontent = null;
    final PrintWriter writer = response.getWriter();

    try
    {
        outStream = new FileOutputStream( new File( path + fileName ) );
        filecontent = filePart.getInputStream();

        int read = 0;
        final byte[] bytes = new byte[1024];

        while( (read = filecontent.read( bytes )) != -1 )
        {
            outStream.write( bytes, 0, read );
        }
        out.println( "Successfully uploaded file." );

    }
    catch( FileNotFoundException e )
    {
        out.println( "<br/> ERROR: " + e.getMessage() );

    }
    finally
    {
        if( outStream != null )
        {
            outStream.close();
        }
        if( filecontent != null )
        {
            filecontent.close();
        }
        if( writer != null )
        {
            writer.close();
        }
    }
}

Web.xml

<web-app 
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>TMS</description>
<resource-ref>
<description>Oracle Datasource</description>
<res-ref-name>jdbc/TMS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
FMC
  • 650
  • 12
  • 31
  • Are you running it in a Servlet 3.x complaint Servlet container? Also, how have you configured your Servlet (either on `web.xml` or annotations)? – Buhake Sindi Jun 17 '15 at 14:48
  • @BuhakeSindi Thanks for replying. I've added the web xml. I am running Apache Tomcat/7.0.59 with Java 1.7.0_75-b13. – FMC Jun 17 '15 at 14:54
  • You haven't configured your Servlet in `web.xml`. Did you configure it using `@WebServlet` annotation? – Buhake Sindi Jun 17 '15 at 15:42
  • Hi @BuhakeSindi you were right. I added MultipartConfig from this tutorial and its working now. Thanks! http://docs.oracle.com/javaee/6/tutorial/doc/gmhal.html – FMC Jun 17 '15 at 15:57

0 Answers0