Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Sending image through socket server causes image on the other end to be corrupt

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Sending anything other than a .txt file through socket causes the file to be corrupt

    I'm sending an image of about 800 kB through a socket server to a client,

    This is the server code I'm using:

            try
            {
                File myfile = new File("oeh.jpg");
     
     
                byte [] data = new byte[(int)myfile.length()];
                BufferedInputStream in = new BufferedInputStream(new FileInputStream(myfile));
     
     
                in.read(data);
     
                out = socket.getOutputStream();
                out.write(data);
                out.flush();

    When I check the amount of bytes in windows, it's a perfect match, what am I doing wrong here?

    Photoshop is telling me:

    "Could not complete your request because the JPEG marker segment length is too short, the file may be truncated or incomplete"

    Sending a rar also causes the file header to be corrupt
    Last edited by vortexnl; February 14th, 2011 at 04:21 PM.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Sending image through socket server causes image on the other end to be corrupt

    read(byte[] b) : "Reads some number of bytes from the input stream and stores them into the buffer array b."
    Some bytes != all bytes.
    use read(byte[] b, int off, int len) , where off = 0 and len is your file len.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sending image through socket server causes image on the other end to be corrupt

    Never mind, I fixed it by using DataInputStream and DataOutputStream

Similar Threads

  1. client/server socket
    By Java_dude_101 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 18th, 2011, 01:16 AM
  2. Sending object through socket
    By Alexandrinne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 16th, 2010, 02:18 AM
  3. sending objects from client to server
    By 11moshiko11 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 8th, 2010, 04:47 AM
  4. Client Socket on Application Server
    By xevimaresma in forum Java Theory & Questions
    Replies: 0
    Last Post: April 12th, 2010, 07:00 AM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM