7 Replies Latest reply: Apr 24, 2007 9:51 AM by 807606 RSS

    Downloading multiple files from ftp

    807606
      Hi everybody i'm back. My problem is now I can download 1 file but when i'm selecting 2 rows and want to download the files i get this error: java.lang.NullPointerException. Here are some lines i got by the System out.methods.

      Lines i get for downloading 1file:
      max1
      Value:1.jpg
      Root:/web/../..
      /web/../../1.jpg
      Directory:/web/../..
      In:org.apache.commons.net.io.SocketInputStream@ee3aa7
      Lines i get downloading more files:
      max2
      Value:1.jpg
      Value:aboutaction.html
      Root:null
      /web/../../1.jpg
      Directory:/web/../..
      In:null
      Error: java.lang.NullPointerException
      So you see the Root is different but i don't know how it comes.

      Here is the code when i pres for downloading:
      else if (evt.getSource() == bDownload)
              {
                   try{
                       int maxRows;
                       int[] selRows;
                       
                            selRows = table1.getSelectedRows();
                            maxRows = table1.getSelectedRowCount();
                            System.out.println("max"+maxRows);
                            if (selRows.length > 0) {
                                 for (int a = 0; a < maxRows; a++){
                                      for (int i= 0; i < 3 ; i++) {
                                           TableModel tm = table1.getModel();
                                           size2 = tm.getValueAt(selRows[a],2);
                                           if (i==1){
                                               value = tm.getValueAt(selRows[a],i);
                                                System.out.println("Value:" + value);
                                                threadDownload((String) value);
                                           }
                                      }
                                      }
                                 }
                       } catch (Exception e){System.out.println(e.toString());}
              }
      Then here is the thread:
      public synchronized void threadDownload(final String valueString) {         
               new Thread(new Runnable() {
                  public void run() {
                       try{
                       wrapper.changeWorkingDirectory(Root);
                       System.out.println("Root:" + wrapper.printWorkingDirectory());
                       Object size = size2;
                      doDownload(valueString,size);
                       }
                       catch (Exception e){}
                  }
              }).start();
          }
      Here is the code for uploading the file:
      public synchronized void doDownload(String myValue,Object size2) {
                try {
                    //wrapper.changeWorkingDirectory(Root);
                     lUpload.setText("Downloading file : " + myValue);
                  lUpload.setVisible(true);
                    aProgressBar.setVisible(true);
                    aProgressBar.setStringPainted(true);
                    bDownload.setEnabled(false);
                  bUpload.setEnabled(false);
                  bMkdirC.setEnabled(false);
                  bMkdirS.setEnabled(false);
                  table.setEnabled(false);
                  table1.setEnabled(false);
                  bFindLoc.setEnabled(false);
                  bFindSer.setEnabled(false);
                    remoteFileName = Root + "/" + myValue;
                    //remoteFileName = myValue;
                    localFileName = path + "/" + myValue;
                    byte[] buffer = new byte[1024];
      
                    String size8 = size2.toString();
                  int size = Integer.parseInt(size8);
                  
                    aProgressBar.setMaximum(100);
                    
                    FileOutputStream out = new FileOutputStream(localFileName);
                    System.out.println(remoteFileName);
                    System.out.println("Directory:" + wrapper.printWorkingDirectory());
                    InputStream in = wrapper.retrieveFileStream(remoteFileName);
                    System.out.println("In:"+in);
                    int counter = 0;
                  double bytesDown = 0;
                  int percVal = 0;
                  
                  while ((counter = in.read(buffer)) >= 0 ) {
                       
                      bytesDown += counter;
                      out.write(buffer,0,counter);
                      percVal = (int) ((bytesDown / size) * 100); 
                      aProgressBar.setValue(percVal);
                      aProgressBar.setString("" + percVal + "%");
                      if (percVal == 100){
                           refreshDownload();
                           refreshUpload();
                      }
                  }
                  out.close();
                     in.close();
               } catch (Exception ex) {
                    System.out.println("Error: " + ex.toString()+ " message: " + ex.getMessage());
               }
          }
      Hope you can help me. You will be awarded

      Satanduvel
        • 1. Re: Downloading multiple files from ftp
          807606
          I don't know what your wrapper is, but the fact that you seem to share the same wrapper among your dowloader threads is probably the cause of the problem.
          In fact both threads interact with this wrapper simultaneously (e.g. changing parameters, getting file stream(s), etc.)

          Can't you get one wrapper for each thread ?
          If not, I guess there is no need for threads, and you should download files sequentially.
          • 2. Re: Downloading multiple files from ftp
            807606
            Thank you 4 your reply but thats not the problem because i syncronize the threats. (For uploading multiple files it works with sharing the wrapper over the threats when i syncronize)

            Do you see another problem maybe?

            Satanduvel
            • 3. Re: Downloading multiple files from ftp
              807606
              Thank you 4 your reply but thats not the problem
              because i syncronize the threats.
              Actually, the way you synchronized things doesn't prevent the second thread from changing wrapper's root directory while first thread is executing run/doDownload.
              Note that you seem to put a lot of efforts in trying to avoid parallel downloads. So, why using threads then (i.e. why not bypassing threadDownload) ?
              (Anyway, what happens when you move the call to changeWorkingDirectory into the doDownload method ?)
              • 4. Re: Downloading multiple files from ftp
                807606
                I'm using the thread for showing a downloadprogressbar so that's why i don't bypass. Without threats is relative easy to do multiple downloads but i have to have a progressbar.
                And when i'm putting the changeWorkingDirectory in the doDownload method i get the same error.

                Thx 4 reply.
                I hope somebody can help me further

                Satanduvel
                • 5. Re: Downloading multiple files from ftp
                  807606
                  Somebody that can help me because my deadline is comming closer.

                  Pls you will be rewarded with 10 stars

                  Satanduvel
                  • 6. Re: Downloading multiple files from ftp
                    807606
                    Here is my proposal.
                    Note that several parts of your code are still unclear to me, so things might be missing.
                    Basically, the idea is to do the job in a separate thread.
                    Event handler simply creates and starts a thread:
                    else if (evt.getSource() == bDownload) {
                        
                        new Thread(new Runnable() {
                            public void run() {
                                doDownload();
                            }
                        }).start();
                    }
                    Then the doDownload method does the whole job (download the files sequentially):
                    public synchronized void doDownload() {
                        
                        bDownload.setEnabled(false);
                        // + disable other components (?)
                        
                        // init progress bar (?)
                        aProgressBar.setValue(0);
                        aProgressBar.setStringPainted(true);
                        aProgressBar.setVisible(true);
                    
                        // Iterate through selected rows
                        int[] selRows = table1.getSelectedRows();
                        TableModel tm = table1.getModel();
                        for(int selRow = 0; selRow < selRows.length; selRow++) {
                    
                            // Get file name & size from current row
                            int fileSize = Integer.parseInt(tm.getValueAt(selRows[selRow],2).toString());
                            String fileName = tm.getValueAt(selRows[selRow],1).toString();
                    
                            // init progress bar & GUI elements
                            aProgressBar.setMinimum(0);
                            aProgressBar.setMaximum(fileSize);
                            aProgressBar.setValue(0);
                            lUpload.setText("Downloading file : " + fileName);
                            lUpload.setVisible(true);
                    
                            // download file
                            try {
                                FileOutputStream out = new FileOutputStream(path + "/" + fileName);
                                InputStream in = wrapper.retrieveFileStream(Root + "/" + fileName);
                                int counter = 0;
                                int bytesDown = 0;
                                byte[] buffer = new byte[1024];
                                while ((counter = in.read(buffer)) >= 0 ) {
                                    bytesDown += counter;
                                    out.write(buffer,0,counter);
                                    aProgressBar.setValue(bytesDown);
                                }
                                out.close();
                                in.close();
                            } catch (Exception e) {
                                // download failed
                                e.printStackTrace();
                            }
                        }
                        refreshDownload(); // ?
                        refreshUpload(); // ?
                    }
                    I hope it's worth a try...
                    • 7. Re: Downloading multiple files from ftp
                      807606
                      It looks like it's working thanks a lot you're a hero

                      You earned the stars

                      Satanduvel