0

I wrote a class, that uploads a file to a FTP server perfectly. Now I want to write the actual progress of the upload into a text file. How do I get the transferred bytes ? I tried the following method:

new CopyStreamAdapter() {

            @Override
            public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {

                File uploadfile = new File(path);

                   try{
                        PrintWriter writer = new PrintWriter("C:\\Users\\lsp\\Desktop\\status.txt", "UTF-8");
                        writer.println((int)(totalBytesTransferred/uploadfile.length()) * 100);
                        writer.close();
                    } catch (IOException e) {
                       System.out.println("Fehler beim Schreiben der Datei.");
                    }
            }

But Im not allowed to use it at the moment.

  • Can you not just query the size file on server using a timer? Every x (milli)seconds check it and if changed then add a new line to your text file? Just the simplest way I can think of. _"How do I get the transferred bytes"_ depends on your uploading code. Is it a `While` loop sending data packets via `socket`? Could that loop also not update textfile? At least check what size packets (32kb? 64kb?) are being sent at a time and just increment the `totalBytesSent += thisPacketsize`... Your shown is not enough to hep you. I don't know why do `uploadfile.length * 100` – VC.One Apr 26 '17 at 21:46
  • I got a simpler solution, Im gonna add it later (works with an InputStream). –  Apr 27 '17 at 09:50
  • You uploading to a server, the correct way is to keep track of bytes sent by packet size. With no clue how your upload code looks it's hard to say "put this here and fix that there" etc. Anyways you have a simpler solution that works for you so we'll leave it at that. – VC.One Apr 27 '17 at 10:04
  • I made it with a simple InputStream, that gets started right in front of the upload and gets closed after upload ist finished - simple :) –  Apr 30 '17 at 18:48

0 Answers0