GeeCON Prague 2014*
The moose likes Servlets and the fly likes Posting a file to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Posting a file to a servlet" Watch "Posting a file to a servlet" New topic
Author

Posting a file to a servlet

Sylvain Mainguy
Greenhorn

Joined: Jan 24, 2002
Posts: 5
Hi
I'm trying to post a file to my servlet, but I encounter problems writing back the file on the server.
I get different response code where I only set one, and not all files are created or fully filled.
This is the code that send the file to the servlet (seems to work properly) :
-------------------------------------------------------------
httpURLConnection = (HttpURLConnection)this.url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("distantPath", "the servlet relative path");
httpURLConnection.setRequestProperty("filename", "the file name");
httpURLConnection.setRequestProperty("Content-type", "application/octet-stream");
httpURLConnection.setRequestProperty("Content-length", "the file length");
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
//loop sendinf the file
bos.flush();
bos.close();
bis.close();
System.out.println(httpURLConnection.getResponseCode() + " " + httpURLConnection.getResponseMessage());
httpURLConnection.disconnect();
-------------------------------------------------------------
Now, this is the servlet code :

When I try writing a byte to the OutputStream, it works fine.
When I get the ServletInputStream and I use the loop, sometimes it works,
sometimes not, with a kind of cyclic different response codes.
Sending 3 files, I will get :
200 OK
209 Unknown -> my code, it has worked
512 Unknown -> my code, it has not worked
The servlet throws this exception :
java.io.IOException: Unexpected command 31
at allaire.jrun.jrpp.JrppInputStream.read(ProxyEndpoint.java, Compiled Code)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at allaire.jrun.ServletIn.read(ServletConnection.java:596)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at SimpleServlet.doPost(SimpleServlet.java, Compiled Code)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:772)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java, Compiled Code)
at allaire.jrun.servlet.JRunNamedDispatcher.forward(JRunNamedDispatcher.java:34)
at allaire.jrun.servlet.Invoker.service(Invoker.java:84)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1013)
at allaire.jrun.servlet.JRunSE.runServlet(JRunSE.java, Compiled Code)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:88)
at allaire.jrun.servlet.JRunSE.service(JRunSE.java:1131)
at allaire.jrun.servlet.JvmContext.dispatch(JvmContext.java:330)
at allaire.jrun.jrpp.ProxyEndpoint.run(ProxyEndpoint.java:354)
at allaire.jrun.ThreadPool.run(ThreadPool.java, Compiled Code)
at allaire.jrun.WorkerThread.run(WorkerThread.java, Compiled Code)
If someone can tell me what is the problem, it would really help me.
Thanks.
Sylvain

I added the UBB code tags to make the source a little easier to read. -- Carl
[ February 01, 2002: Message edited by: Carl Trusiak ]
Sylvain Mainguy
Greenhorn

Joined: Jan 24, 2002
Posts: 5
Hi
As you can see, I reply to myself, cause I think it could help.
The solution to this problem is that it is not a good idea to close the ServletInputStream, since I didn't open it. Just remove the line bis.close() and everything will work fine !!
 
GeeCON Prague 2014
 
subject: Posting a file to a servlet