简体   繁体   中英

Sending a file over java socket

I have been searching Google for hours trying to figure how to successfully send any file over a java socket. I have found many things online but none of them seem to work for me, I finally came across Object Output/Input streams to send files between the server and client, it is so far the only thing I have found that has worked, however it only seems to work over a connection to the localhost. For instance if I connect to the server from a friend's computer on a different network and send a file it fails, socket closes saying "read timeout" and then shortly after reconnects, file never gets sent. If I connect to localhost on the server and send the file it works perfectly. So whats the problem and how might I fix it?

Edit: below is the updated code.. moves extremely slow

public synchronized File readFile() throws Exception {
    String fileName = readLine();
    long length = dis.readLong();
    File temp = File.createTempFile("TEMP", fileName);
    byte[] buffer= new byte[1000];
    int count=0;
    FileOutputStream out = new FileOutputStream(temp);
    long total = 0;
    while (total < length && (count = dis.read(buffer, 0, (int)Math.min(length-total, buffer.length))) > 0)
    {
        System.out.println(total+" "+length+" "+temp.length()); //Just trying to keep track of where its at...
        out.write(buffer, 0, count);
        total += count;
    }
    out.close();
    return temp;
}


public synchronized void writeFile(File file) throws Exception {
    writeString(file.getName());
    long length = file.length();
    dos.writeLong(length);
    byte[] buffer= new byte[1000];
    int count=0;
    FileInputStream in = new FileInputStream(file);
    long total = 0;
    while (total < length && (count = dis.read(buffer, 0, (int)Math.min(length-total, buffer.length))) > 0)
    {
        System.out.println(total+" "+length); //Just trying to keep track of where its at...
        dos.write(buffer, 0, count);
        total += count;
    }
    in.close();
}

Both client and server have writeFile and readFile. At the moment I am using this to send and display images.

Don't use object streams for this. Use DataInputStream and DataOutputStream:

  1. Send and receive the filename, with writeUTF() and readUTF() .
  2. Send and receive the data, as follows:

     while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } 

    You can use this code at both ends. buffer can be any size greater than zero. It doesn't need to be the size of the file, and that doesn't scale anyway to large files.

  3. Close the socket.

If you need to send multiple files, or have some other need to keep the socket open:

  1. Send the length ahead of the file, with writeLong() , and read it with readLong() and amending the loop, as follows:

     long length = in.readLong(); long total = 0; while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0) { out.write(buffer, 0, count); total += count; } 

    and don't close the socket. Note that you have to test total before reading, and that you have to adjust the read length parameter so as not to overrun the end of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM