Results 1 to 3 of 3
- 04-23-2012, 06:30 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
SevletJava Code:Foto<input type="file" name="foto" />
ErroJava 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"));
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
- 04-24-2012, 09:27 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 05-31-2012, 09:49 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 41
- Rep Power
- 0
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
-
simple form & Servlet help
By bhavinragha in forum Java ServletReplies: 4Last Post: 01-27-2012, 07:42 PM -
<html:form> struts form not working if it is inside a jsp under WEB-INF
By dariusaron in forum Web FrameworksReplies: 6Last Post: 04-06-2011, 12:39 PM -
use Servlet submit the form to receive a form for the Chinese but it shows the garble
By dmt198704 in forum Java ServletReplies: 0Last Post: 10-31-2009, 09:24 AM -
how can i link html+servlet+xml to form servlet
By balachandarr in forum Java ServletReplies: 0Last Post: 04-15-2009, 04:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks