0

I made a code that sending files from one computer to another, the problem is that after one sending its not working anymore. I know that the problem is when i'm writing to the writer but I don't know why its not working.

client:

    File file =new File(path);
    long fileSize = file.length();
    long completed = 0;
    int step = 150000;
    Request req = new Request(RequetType.DOWNLOAD_FILE,file.getName());
    writer.writeObject(req);
    writer.flush();
    // creates the file stream
    FileInputStream fileStream = new FileInputStream(file);

    // sending a message before streaming the file
   // writer.writeObject("SENDING_FILE|" + file.getName() +"|" + fileSize);
    writer.reset();
    byte[] buffer = new byte[step];
    while (completed <= fileSize) {
        fileStream.read(buffer);
        writer.write(buffer);
        completed += step;
    }
    System.out.println(completed);
    //writer.writeObject("SEND_COMPLETE"); 
    fileStream.close();

server:

                String filename = (String)req.getContent();

                try {
                    FileOutputStream outStream =new FileOutputStream(Startdir+""+filename);
                    byte[] buffer = new byte[200000];
                    int bytesRead = 0, counter = 0;

                    bytesRead = this.reader.read(buffer);  
                    if (bytesRead >= 0) {
                        outStream.write(buffer, 0, bytesRead);
                        counter += bytesRead;
                        System.out.println("total bytes read: " +
                                                        counter);
                    }
                    if (bytesRead < 1024) {
                        outStream.flush();
                    }
                    while (true) 
                    {
                        bytesRead = this.reader.read(buffer);
                        if (bytesRead >= 0) {
                            outStream.write(buffer, 0, bytesRead);
                            counter += bytesRead;
                            System.out.println("total bytes read: " +
                                                            counter);
                        }
                        if (bytesRead ==0) 
                        {
                            outStream.flush();
                            break;
                        }
                    }
                    System.out.println("Sent:"+filename+"  from:"+MainApp.computersconnection.getIp());

                } catch (Exception e) {
                    System.out.println("Error on downloading file!");
                }
Stuart Siegler
  • 1,686
  • 4
  • 30
  • 38
  • Hint: what happens in the files is not an exact multiple of the step size? (on the writing side) And if `completed == filesize` what are you reading (after the end of the file)? – Peter Lawrey Apr 22 '14 at 16:34
  • i dont reading nothing after the end of the file,when completed == filesize its meen that the sending end. – user3561290 Apr 22 '14 at 16:47
  • What do you mean you are "sending end"? You appear to be reading another buffer and writing it. – Peter Lawrey Apr 22 '14 at 16:53
  • i cant use another buffer, beacuse what you see in the client is action when starting when i'm click on buttom – user3561290 Apr 22 '14 at 16:57
  • I am suggesting; why are you attempting to do anything if you have read all the data? (I didn't mention another buffer) – Peter Lawrey Apr 22 '14 at 17:01

1 Answers1

0

You need to flush the streams in the end even if the file isn't 0 bytes long. Try implementing that change and tell me if it still gives you trouble.

(Flush the output stream when your done sending a file).

ObedMarsh
  • 433
  • 4
  • 19
  • +0 I can almost guarentee this will make no difference even if the stream is buffered as the byte[] is too large to put in the buffer. If it's not buffered (and it shouldn't be) it will definitely make no difference. – Peter Lawrey Apr 22 '14 at 17:02