Frage

I am trying to transfer a large file from a server to a client. My code so far works but only if I set the buffer size in the client code to the exact size of the file. I won't always know what the file size is going to be. I keep finding examples that claim it doesn't matter what the size of the file or buffer is because it will just keep reading from the input stream...? However, when I implement the code that supposedly does this, it transfers 0 bytes.

Client:

public static void main (String [] args ) throws IOException {
    int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    Socket sock = null;
    try {
    sock = new Socket(hostname, 20000);
    System.out.println("Connecting...");

    // receive file
    InputStream is = sock.getInputStream();
    fos = new FileOutputStream(FILE_TO_RECEIVE);
    bos = new BufferedOutputStream(fos);
    //////////// replaced this ////////////////////////////
    byte[] buffer  = new byte[BUFFER_SIZE];
    bytesRead = is.read(buffer,0,buffer.length);
    current = bytesRead;
    do {
       bytesRead = is.read(buffer, current, (buffer.length-current));
       if(bytesRead >= 0){ current += bytesRead;}
    } while(bytesRead > 0);
    bos.write(buffer, 0 , current);
    ///////////////////////////////////////

    // with this:
    //        int count;
    //        byte[] buffer = new byte[8192];
    //        while ((count = is.read(buffer)) > 0)
    //        {
    //          bos.write(buffer, 0, count);
    //        }
    //////////// transfers 0 bytes ///////////

    bos.flush();
    System.out.println("File " + FILE_TO_RECEIVE
        + " downloaded (" + current + " bytes read)");
  }
  finally {
    if (fos != null){ fos.close();}
    if (bos != null){ bos.close();}
    if (sock != null){ sock.close();}
  }
}

Server:

public static void main(String[] args) throws IOException{
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {

        servsock = new ServerSocket(20000);
        while (true) {
            System.out.println("Waiting...");
            try {
                sock = servsock.accept();
                System.out.println("Accepted connection : " + sock);
                // send file
                File myFile = new File (FILE_TO_SEND);
                byte [] mybytearray  = new byte [(int)myFile.length()];
                fis = new FileInputStream(myFile);
                bis = new BufferedInputStream(fis);
                bis.read(mybytearray,0,mybytearray.length);
                os = sock.getOutputStream();
                System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
                os.write(mybytearray,0,mybytearray.length);
                os.flush();
                System.out.println("Done.");
            }
            finally {
                if (bis != null){ bis.close();}
                if (os != null){ os.close();}
                if (sock!=null){ sock.close();}
            }
        }
    }
    finally {
        if (servsock != null){ 
            servsock.close();
        }
    }
}

Thank you for your help

War es hilfreich?

Lösung

Your copy loops are both different, and both nonsense. One of them isn't even a loop. Try this:

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

This works for any buffer of size >= one byte.

Use this at both ends.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top