• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Posting a file to a servlet

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic