473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java Binary I/O And Networking Question

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
1 11711

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
2
by: Jim G. | last post by:
I'm looking to build a java application like you see on sports websites, where the information is updated live, for instance in baseball the balls, strikes, etc. are updated. Any good points of...
1
by: Nuno Silva | last post by:
Hello, I'm starting to learn about java networking. What I want to do is for now, just make a server that listens to connections through a port, and accepts it's connections from the client. ...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
5
by: sea | last post by:
I have 2 applications, one a Microsoft Visual Basic application and the other a Java application both connecting to the same DB2 database. The database has binary LOB objects. The Visual Basic...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
63
by: s0suk3 | last post by:
I've been programming Python for a couple of years now. Now I'm looking to move on to either C++ or Java, but I'm not sure which. Which one do you think will be a better transition for a Python...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.