This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Servlets and the fly likes Strange behaviour in HttpURLConnection to Servlet communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Strange behaviour in HttpURLConnection to Servlet communication" Watch "Strange behaviour in HttpURLConnection to Servlet communication" New topic
Author

Strange behaviour in HttpURLConnection to Servlet communication

Ioan Berciu
Greenhorn

Joined: Nov 17, 2002
Posts: 15
Hi!

I want to upload pictures on a server using HttpURLConnection.

My servlet is deployed in Tomcat 5.5.17.

The problem is that the upload never stops and the wrong data is uploaded.

On the server the uploaded value is allways 255 for each byte.

I belive that the communication is binary.

Did anyone encounterd this problem?
Any help will be appreciated.

Thank you.


Servlet code:




Client code:



web.xml descriptor:



JOE
Ioan Berciu
Greenhorn

Joined: Nov 17, 2002
Posts: 15
It apears that the first byte that was read from the file is sent over and over to the server.

Couldn't found out more.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42356
    
  64
int msg = is.read();
while(msg != -1)
{
fos.write(msg);
}


You're only ever reading the first byte. What you want is probably something like (haven't tested it):

int msg;
while((msg = is.read()) != -1)
{
fos.write(msg);
}


Ping & DNS - my free Android networking tools app
Ioan Berciu
Greenhorn

Joined: Nov 17, 2002
Posts: 15
That was it.

Sometimes is right infront of you and you just can't see it.

Thank you Ulf.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Strange behaviour in HttpURLConnection to Servlet communication