• Post Reply Bookmark Topic Watch Topic
  • New Topic

URLConnection posting to servlet  RSS feed

 
Sylvain Mainguy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm trying to upload a file to my servlet, using an URLConnection.
The servlet's address is http://serveur/pa/servlet/SimpleServlet.
The URLConnection is connected to http://serveur/pa/servlet/SimpleServlet.
The file to post is newFile.
I try :
FileInputStream fis = new FileInputStream(newFile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = urlConnection.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
I write on the stream, using bos.write((...).getBytes("UTF-8")) :
"POST " + "/pa/servlet/SimpleServlet" + " HTTP/1.1\r\n"
"distantPath: " + "/Upload" + "\r\n"
"filename: " + newFile.getName() + "\r\n"
"Content-type: application/octet-stream\r\n"
"Content-length: " + newFile.length() + "\r\n"
"\r\n"
I'm not sure about the address after POST. This code compile properly, but doesn't seem to work (my servlet doesn't receive anything).
In fact I'm not sure about anything now ! The code looks like all examples.
Please help me !!
 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default the URLConnection expects to be reading,
not reading and writing.
According to the Javadoc API for 1.3:
In general, creating a connection to a URL is a
multistep process:
1.The connection object is created by invoking the
openConnection method on a URL.
2.The setup parameters and general request
properties are manipulated.
3.The actual connection to the remote object is
made, using the connect method.
4.The remote object becomes available. The header
fields and the contents of the remote object can
be accessed.
I think you need to work with step 2, calling
setDoOutput() on your URLConnection before you
call connect(). (Or before you call another method
that calls connect() for you).
HTH,
Joe
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create a URLConnection using
"http://serveur/pa/servlet/SimpleServlet" what you get is in fact a java.net.HttpURLConnection. This object handles creating and writing HTTP headers, and the default method is GET. So your connection will go to the doGet method.
You can set the method to POST and the other header properties with methods in HttpURLConnection and URLConnection.
Only if you are working directly with sockets would you write your own HTTP headers.
Bill
 
Sylvain Mainguy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Thanks to both of you.
I had not understood that requestProperty was for requestHeader. Now it works fine.
Thank you.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!