0

I'm uploading a file on FTP using Apache and Async. The problem is that on big files (i.e. 200 Mb) the progress bar restarts its progress but the upload is continuing (I checked on FTP from computer).

This is what I see in Logcat:

1->2->3->4->5->6->7->8-> -8 -> -7 .... to 8 and starts again

Async Background code:

con = new FTPClient();
con.setConnectTimeout(300000);
con.setDataTimeout(300000);
// con.setKeepAlive(true);

con.connect("host IP",21);                
if (con.login("user", "password"))
{
    con.enterLocalPassiveMode(); // important!
    con.setFileType(FTP.BINARY_FILE_TYPE);

    boolean directoryExists = con.changeWorkingDirectory(Email);
    File CloudVersionSysForRestore = new File(getFilesDir()+File.separator+"Cloud Version.itb");

    try {
        CloudVersionSysForRestore.createNewFile();
        OutputStream OutstreamCloud = new FileOutputStream(new File(CloudVersionSysForRestore.getPath()));                                  
        con.retrieveFile("/"+Email+"/Cloud Version.itb", OutstreamCloud);

        OutstreamCloud.close();

        if(x!=2){
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(getFilesDir()+File.separator+"Cloud Version.itb")));
            String read;
            StringBuilder builder = new StringBuilder("");

            while((read = bufferedReader.readLine()) != null) {
                Log.w("READ", read);
                builder.append(read);
            }
            bufferedReader.close();

            long getintfromstring = Long.parseLong(builder.toString());           
            if(getintfromstring<Version){
                Log.w("Version", String.valueOf(Version));
                try{
/************************************ UPLOAD + Progress ****************************/
                    long fileSize = DBToUploadFromInternalStorage.length();
                    int sentBytes = 0;
                    InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);        
                    System.out.println("Start uploading second file");
                    OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb");
                    byte[] bytesIn = new byte[4 * 1024];
                    int read1 = 0;                           
                    while ((read1 = inputStream.read(bytesIn)) !=-1) {
                        outputStream.write(bytesIn, 0, read1);
                        sentBytes+=read1;
                        final int progress = (int) ((sentBytes * 100) / fileSize);

                        runOnUiThread(new Runnable() {
                            public void run() {
                                pd.setProgress(progress);
                                Log.w("Progress", String.valueOf(progress));
                            }
                        });
                    }                                 
                    outputStream.close();
                    inputStream.close();

/************************************ UPLOAD + Progress ****************************/
                }
                catch(Exception e){
                    e.printStackTrace();
                    x=2;
                }

                boolean completed = con.completePendingCommand();

For Long is it like this?

/************************************ UPLOAD + Progress ****************************/
                                                 long fileSize = DBToUploadFromInternalStorage.length();
                                                 long sentBytes = 0;
                                                 InputStream inputStream = new FileInputStream(DBToUploadFromInternalStorage);    

                                                     System.out.println("Start uploading second file");
                                                     OutputStream outputStream = con.storeFileStream("PilotLogbookDB.itb");
                                                     byte[] bytesIn = new byte[4 * 1024];
                                                     int read1 = 0;                           
                                                     while ((read1 = inputStream.read(bytesIn)) !=-1) {

                                                         outputStream.write(bytesIn, 0, read1);
                                                         sentBytes+=read1;
                                                         final long progress = (long) ((sentBytes * 100) / fileSize);



                                                         runOnUiThread(new Runnable() {
                                                             public void run() {

                                                                 pd.setProgress(Long.bitCount(progress));
                                                                 Log.w("Progress", String.valueOf(progress));




                                                             }
                                                         });
                                                     }


                                                     outputStream.close();
                                                     inputStream.close();








                                               /************************************ UPLOAD + Progress ****************************/
Iosif
  • 166
  • 1
  • 15

1 Answers1

0

You're using an int as progress counter (sentBytes), however in the line final int progress = /*...*/ you multiply the progress counter by 100, which reduces the actual number of bytes you can use by factor 100, you should get a higher possible value by changing it to:

final int progress = (int) ((((double) sentBytes) / fileSize)  * 100);

Alternately, if you have much larger files (> 2 GB), you may prefer to use a long.

dst
  • 3,307
  • 1
  • 20
  • 27
  • I added to question long version... is it ok like this? – Iosif Aug 02 '13 at 23:06
  • No, in what i wrote (regarding Long) is wrong, it should be: pd.setProgress((int) progress); – Iosif Aug 02 '13 at 23:23
  • `Long.bitCount(progress)` is not what you want I guess. As your size is a `long` now, you fixed the issue, now `(int) ((sentBytes * 100) / fileSize)` gives you a proper integer between 0 and 100. As that fits into an int, you can use `final int progress = (int) ((sentBytes * 100) / fileSize)`. – dst Aug 02 '13 at 23:28
  • the progress is on, the file is ok, but the dialog is not dismissing (on large files - tested on upload, now testing for download), which do you think is the problem? :| – Iosif Aug 03 '13 at 00:02
  • I haven't worked with progress dialogs yet, but I assume you're not calling dismiss anywhere? At least not in the code you posted... – dst Aug 03 '13 at 00:09
  • i call it in on post execution – Iosif Aug 03 '13 at 00:11
  • Did you ensure that the execution completed? I'd recommend you put some logging in there as well, just to ensure all calls are as you think they are. I can only guess from what I see in here... you could also try to debug the application, step through and maybe the issue comes to your mind ;) – dst Aug 03 '13 at 00:15
  • is it possible the server to disconnect me? but the file looks ok :| – Iosif Aug 03 '13 at 00:27
  • I don't know your whole application and thus cannot comment on that. Try to debug the issue, or ask a new, specific question about your new problem (with suitable parts of code). – dst Aug 03 '13 at 00:37
  • i have an ideea, i'll try to see the problem with try {} catch(Exception e){} – Iosif Aug 03 '13 at 08:09