1

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();
    }   

}

E.P
  • 13
  • 4

1 Answers1

0

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.

  • I dont really understand why the fis.read(buffer,0,buffer.lenght) solved the problem but really thanks you for you answer :) If you can explain my again how dos work ? thx – E.P Feb 12 '17 at 10:15
  • Yes. It's really not that complicated. You did a new byte[(int) f.length()]. What that does is allocating a memory area for storing f.length() bytes. It does not put any data into those bytes of memory. In most cases the array would be initialized to bytes of zeros (binary zeros, so would often show up as strange characters in an editor) by the jvm. To actally put data from your input file into your newly created array you need to do the fis.read. – Jan-Espen Oversand Feb 13 '17 at 10:31