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

    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...


    cadastro.jsp
    Java Code:
    Foto<input type="file" name="foto" />
    Sevlet
    Java Code:
    String fotoURL = req.getParameter("foto");
    		System.out.println(fotoURL);
    		URL urlFoto = null;
    		urlFoto = getClass().getClassLoader().getResource(fotoURL);  
    		
    		
    		System.out.println(urlFoto);
    		BufferedImage foto = ImageIO.read(urlFoto);
    		
    		ImageIO.write(foto, "JPG", new File("Imagens\\ManagerFotos\\" + nick + ".jpg"));
    Erro
    Java Code:
    CIMG5912.JPG
    null
    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 javax.imageio.ImageIO.read(Unknown Source)
    	at tlm.servlet.ManagerCadastrar.service(ManagerCadastrar.java:52)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at tlm.conexao.Filtro.doFilter(Filtro.java:48)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

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

    Leo

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    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
    Posts
    41
    Rep Power
    0

    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, 07:42 PM
  2. Replies: 6
    Last Post: 04-06-2011, 12:39 PM
  3. Replies: 0
    Last Post: 10-31-2009, 09: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, 04: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
  •