Results 1 to 3 of 3
  1. #1
    LeoKringer is offline Member
    Join Date
    Apr 2012
    Rep Power

    Default Getting a image from a form and working it in a Servlet

    Good Afternoon,

    I have a form and one of the fields is a file field where I load a image. This form is sent to a Servlet where I'd get it and save this image inside a folder in my application.

    The problem happens when I try to read the image in the servlet. It's backing NULL...

    Java Code:
    Foto<input type="file" name="foto" />
    Java Code:
    String fotoURL = req.getParameter("foto");
    		URL urlFoto = null;
    		urlFoto = getClass().getClassLoader().getResource(fotoURL);  
    		BufferedImage foto =;
    		ImageIO.write(foto, "JPG", new File("Imagens\\ManagerFotos\\" + nick + ".jpg"));
    Java Code:
    23/04/2012 13:15:24 org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: Servlet.service() for servlet [ManagerCadastrar] in context with path [/TLMUP] threw exception
    java.lang.IllegalArgumentException: input == null!
    	at Source)
    	at tlm.servlet.ManagerCadastrar.service(
    	at javax.servlet.http.HttpServlet.service(
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    	at tlm.conexao.Filtro.doFilter(
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    	at org.apache.catalina.core.StandardWrapperValve.invoke(
    	at org.apache.catalina.core.StandardContextValve.invoke(
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
    	at org.apache.catalina.core.StandardHostValve.invoke(
    	at org.apache.catalina.valves.ErrorReportValve.invoke(
    	at org.apache.catalina.valves.AccessLogValve.invoke(
    	at org.apache.catalina.core.StandardEngineValve.invoke(
    	at org.apache.catalina.connector.CoyoteAdapter.service(
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$ Source)
    	at Source)

    I have no ideia how to fix this problem. Thanks for any help...


  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Getting a image from a form and working it in a Servlet

    The server is trying to access the file based on treating the filename parameter ("foto") as the filepath on the server.
    The server cannot read stuff that's on the client.
    So you need to read the request stream to get the content.
    Normally I recommend using something like Apache FileUpload to handle all this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    nicephotog is offline Member
    Join Date
    Mar 2012
    Rep Power

    Default Re: Getting a image from a form and working it in a Servlet

    agreed "Apache FileUpload" but it does not stop there.
    When the file e.g. JPG is uploaded, you only need to write the bytes of the file to the file location you want to store it.
    ImageIO class is for a special imaging operation of raw image data and there is no need to put it into a BufferedImage object.
    java.awt.image classes are not required , javax.imageio classes are not required.

    A file you upload is called an "attachment" and the file data is URL encoded by a browser, then wrapped with a "unique per instance prefix token wrapper" around the file data at both ends. "The token-wrapper ends are identical" and contain a random chosen sequence of alphanumeric characters followed by a quantity of equals signs (IETF RFC's).

Similar Threads

  1. simple form & Servlet help
    By bhavinragha in forum Java Servlet
    Replies: 4
    Last Post: 01-27-2012, 08:42 PM
  2. Replies: 6
    Last Post: 04-06-2011, 01:39 PM
  3. Replies: 0
    Last Post: 10-31-2009, 10:24 AM
  4. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 05:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts