954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Send image to client over a socket

Hi all,

I searched this topic and found a couple of posts but I couldn't find what I am looking for. Here's the problem - I am trying to send an image to a client (browser in this case) over a socket. After the image is received I am trying to display it in the browser but it looks corrupted, as if some of the data is missing. The weird thing is that I am able to send some images and display them without any problems. I probably should mention that I am resizing the images, but that's done in HTML so I don't think it's the problem here. Here's what I've tried so far:

1. Send different image type, e.g. png, gif - worked;
2. Save the jpg file as a png and send it over - worked;
3. Write a simple program that copies a jpg image from one location to another - worked;
4. Send the image in actual size to the client - worked.

(by 'worked' I mean that I can see it in the browser)

Here's how I send the data to the client:

BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
// f is the file to be sent to the client.
BufferedInputStream reader = new BufferedInputStream( new FileInputStream( f ) );

// send OK headers and content length using f.length()

byte[] buffer = new byte[ 4096 ];
int bytesRead;
while ( (bytesRead = reader.read(buffer)) != -1 ) {
	out.write( buffer, 0, bytesRead );
}
reader.close();
out.flush();
out.close();

and here's the sample program for file transfer program I wrote:

package main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataSendTester {

	public static void main( String[] args ) {
		File testF = new File( "test.jpg" );
		File result = new File( "TEST/testImg.jpg" );
		
		try {
			System.out.println( "Writing..." );
			BufferedInputStream in = new BufferedInputStream( new FileInputStream( testF ) );
			BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream( result ) );
			
			byte[] buffer = new byte[ 4096 ];
            int bytesRead;
            while ( (bytesRead = in.read( buffer )) != -1 ) {
        		out.write( buffer, 0, bytesRead );
            }
            
        	out.flush();
        	out.close();
			
        	System.out.println( "Done." );
        	
		} catch ( FileNotFoundException e ) {
			e.printStackTrace();
		} catch ( IOException e ) {
			e.printStackTrace();
		}
	}

}


Any ideas? Thanks!

masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 20
Solved Threads: 74
 

Why not use writeObject/readObject via an ObjectOutputSream/ObjectInputStream to send/receive your image in a single call?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

How is the browser asking for the image? Is because of an HTML tag like Does

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Thanks for the replies - I managed to figure it out. The problem was that I was checking for .jpg whereas the original file was .JPG which apparently makes a difference here. I changed it so that the content type also checks .JPG and now it seems to work.

masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 20
Solved Threads: 74
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: