By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Java Binary I/O And Networking Question

P: n/a
Problem: I'm writing a client app. that needs to download an image
file (non-text) from a remote location and save to the client machine.

Here is what I have so far (pseudocode):

import java.io.*
import java.net.*;

//create the socket to the server
Socket client = new Socket(host,port);

//create temp buffer to store the downloaded image
byte [] buffer = new byte[70000];

//bind socket to an output stream for client file request
PrintWriter out = new PrintWriter(client.getOutputStream(), true);

//Send HTTP request for binary file through the above output stream
out.println("GET /images/image.jpg HTTP/1.0\n");

//Create an input stream and bind it with socket for server response
DataInputStream inStream = new
DataInputStream(client.getInputStream());

//Read-in binary file that the server sends back
inStream.read(buffer);

//Write the buffer to a file on the client machine
File f = new File("c:\\images\\image.jpg");
DataOutputStream outStream = new DataOutputStream(new
FileOutputStream(f));
outStream.write(buffer, 0, buffer.length);
client.close();

When I try to view the downloaded image, my viewer says "corrupt
file".
This code works fine when the file being requested is text. I've also
tried stripping the HTTP response header.

RReynolds, FL
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Russ Reynolds" <rr******@aol.com> wrote in message
news:2a**************************@posting.google.c om...
Problem: I'm writing a client app. that needs to download an
image file (non-text) from a remote location and save to the
client machine.

Here is what I have so far (pseudocode):

import java.io.*
import java.net.*;

//create the socket to the server
Socket client = new Socket(host,port);

//create temp buffer to store the downloaded image
byte [] buffer = new byte[70000];

//bind socket to an output stream for client file request
PrintWriter out = new PrintWriter(client.getOutputStream(), true);

//Send HTTP request for binary file through the above output stream
out.println("GET /images/image.jpg HTTP/1.0\n");

//Create an input stream and bind it with socket for server response
DataInputStream inStream = new
DataInputStream(client.getInputStream());

//Read-in binary file that the server sends back
inStream.read(buffer);

//Write the buffer to a file on the client machine
File f = new File("c:\\images\\image.jpg");
DataOutputStream outStream = new DataOutputStream(new
FileOutputStream(f));
outStream.write(buffer, 0, buffer.length);
client.close();

When I try to view the downloaded image, my viewer says
"corrupt file". This code works fine when the file being requested
is text. I've also tried stripping the HTTP response header.


You may wish to try:

//Read-in binary file that the server sends back
inStream.readFully(buffer);

instead of:

//Read-in binary file that the server sends back
inStream.read(buffer);

While not immediately applicable to the current problem you may wish to take
a look at the 'javax.imageio' package - it may be useful in future ?

I hope this helps.

Anthony Borla
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.