Member Avatar for masterofpuppets

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!

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

How is the browser asking for the image? Is because of an HTML tag like <IMG SRC=
Does the browser do an HTTP GET to the server and the server calls your code to send the bytes?
What format is the browser expecting the image bytes to be in?

Member Avatar for masterofpuppets

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.