javafiletcpserverbufferedoutputstream

BufferedOuputStream make Blank file


I have the following code for upload file from client to server tcp but when i try to open manually the file is empty why the good weight.. I have look lot of post on stackOverflow but nothing make change Thx (Sorry for my bad english)
Server:

public class ThreadServer extends Thread{

private Socket soc;
private FileOutputStream fos;
private BufferedOutputStream bos;
private InputStream in;

public ThreadServer (Socket soc) {
    this.soc = soc;
}

public void run(){
    try {
        fos = new FileOutputStream("C:/Users/erwan/workspace/Word/server/text.txt");
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    bos = new BufferedOutputStream(fos);
    byte[] buffer = new byte[1024];
    try {
        in = soc.getInputStream();
        int count = 0;
        while((count= in.read(buffer, 0 , buffer.length)) != -1) {
             System.out.println(count+" octets received...");                 
             bos.write(buffer);
        } 
        bos.flush();
        bos.close();
        in.close();
        soc.close();
        System.out.println("File sent succesfully!");
    }catch(IOException e){
        e.printStackTrace();
        System.out.println("Une erreur est survenu");
    }
}

}

client:

public class Client {
private static Socket as;
private static FileInputStream fis;
private static BufferedInputStream bis;
private static OutputStream out;
public static void main( String[] args ){
    as = null;
    try{
        as = new Socket(InetAddress.getLocalHost(),4020);

        File f = new File (args[0]);
        byte [] buffer  = new byte [(int) f.length()];
        fis = new FileInputStream(f);
        setBis(new BufferedInputStream(fis));
        out = as.getOutputStream();
        System.out.println("uploading...");
        out.write(buffer,0,buffer.length);
        out.flush();
        out.close();
        System.out.println("the file is uploaded.");
        as.close();
    }catch(IOException e){
        e.printStackTrace();
    }   

}


Solution

  • The buffer in client does not seem to be populated with data. It is initialized as an array of bytes with the length of the file, but there are no read method calls done on the input stream. For the purpose of testing a fis.read(buffer) would probably quickly get some data into the buffer. Keep in mind that reads are not guaranteed to fill the whole length of the buffer. So especially if your file contains zeros then the lack of reading actual data into the buffer (of the client) is the likely culprit.

    Other than that the server code also assumes that the read method fully populates the buffer, so the write method call should specify a length (count). So change bos.write(buffer) into bos.write(bos, 0, count). This will probably become apparent at the end of the file (if the file is more than 1024 bytes long), as the end of the file would become a repetition of some of the data from the previous chunk.