1 2 Previous Next 24 Replies Latest reply: Apr 19, 2007 6:01 AM by 807606 RSS

    Upload multiple files to ftp

    807606
      Hello i'm making an applet that has 2 JTables en when you select a file in a JTable you can upload the file. If i select 1 file i can upload it but when i'm selecting 2 or more i get the error org.apache.commons.net.MalformedServerReplyException: Truncated server reply .
      I think what happens is that the thread eachother overides(cross each other) and thats why he give me the fault but i don't know how to solve the problem.

      Here is my code

      So when i click the button threadUpload will be called:
      else if (evt.getSource() == bUpload)
              {
                   try{
                   int maxRows;
                   int[] selRows;
                   
                        selRows = table.getSelectedRows();
                        maxRows = table.getSelectedRowCount();
                        System.out.println("max"+maxRows);
                        if (selRows.length > 0) {
                             for (int a = 0; a < maxRows; a++){
                                  for (int i= 0; i < 3 ; i++) {
                                       if (i==1){
                                            TableModel tm = table.getModel();
                                            value = tm.getValueAt(selRows[a],i);
                                            System.out.println("value:" + value);
                                                 
                                                        threadUpload();
                                            //wait(50000000);
                                       }
                                  }
                             }
                       }
                   } catch (Exception e){}
              }
      Then this is the tread thats starts the uploading
      public void threadUpload() {
                
                t = new Thread(new Runnable() {
                     public void run() {
                      //Get the path + name of the selected File
                      String allText = path+value;
                      System.out.println("filename:" + allText);
                      doUpload(allText);
                  }
                });
                try{
                t.start();
                } catch (Exception e){}
           }
      Then is this the code for uploaden the file:
      public void doUpload(String filename) {
              if (wrapper.isConnected()){
                  try{
                       wrapper.changeWorkingDirectory(Root);
                      wrapper.binary();
                      lUpload.setText("Uploading file : " + value);
                       lUpload.setVisible(true);
                      aProgressBar.setVisible(true);
                      aProgressBar.setStringPainted(true);
                      byte[] buffer = new byte[1024];
                      try {
                           String remoteFile = (String) value;
                           System.out.println("remotefile:"+remoteFile);
                          File f = new File(filename);
                          int size = (int) f.length();
                          FileInputStream in = new FileInputStream(filename);
                          OutputStream out = wrapper.storeFileStream(remoteFile);
                          int counter = 0;
                          double bytesUploaded = 0;
                          int percVal = 0;
                          System.out.println("Root: " + Root);
                          while ((counter = in.read(buffer)) >= 0 ) {
                              bytesUploaded += counter;
                              out.write(buffer,0,counter);
                              percVal = (int) ((bytesUploaded / size) * 100); 
                              aProgressBar.setValue(percVal);
                              aProgressBar.setString("" + percVal + "%");
                              if (percVal == 100){
                              refreshUpload();
                              JOptionPane.showMessageDialog(this,"Uploaden geslaagd","Gelukt",JOptionPane.INFORMATION_MESSAGE);
                              lUpload.setVisible(false);
                              aProgressBar.setVisible(false);
                              }
                          }
                          out.close();
                          in.close();
                      } catch (Exception ex) {
                           System.out.println(ex);
                          JOptionPane.showMessageDialog(this,"Error: " + ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
                          aProgressBar.setVisible(false);
                      }
                  } catch (Exception ex) {
                       System.out.println(ex);
                      JOptionPane.showMessageDialog(this,"Connectie Error: " + ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
                  }
              } else {
                   System.out.println("Geen connectie tijdens het uploaden...");
                   JOptionPane.showMessageDialog(this,"Geen connectie tijdens het uploaden","ERROR",JOptionPane.ERROR_MESSAGE);
                   connect();
                   return;
              }
              postUpload();
          }
      So my opinion is that the treads are being covered by the other when i upload more than 1 file.

      I hope somebody can help me.

      Satanduvel
        • 1. Re: Upload multiple files to ftp
          mlk
          wrapper is shared between threads yes?
          • 2. Re: Upload multiple files to ftp
            807606
            yes, but I have the feeling when the first time threadUpload() is called it's start threading but in de same time the second row get started with another threatUpload() so when i put a sleep of 5000 then 2files sometimes can be uploaded when they are not so big but when the files are a bit bigger or when i select 3 files the same error occurs.

            Thx 4 replying
            • 3. Upload multiple files to ftp
              807606
              Pls help me if your answer works or leads to the solution you get all the 10 duke stars
              • 4. Re: Upload multiple files to ftp
                807606
                A qick and dirty way to solve it would be a restriction to upload one file at a time. I would do something like this
                boolean isUploading = false;
                
                public void doUpload(String filename) {
                        if (wrapper.isConnected()){
                            isUploading = true;
                            ...
                            finally{
                                isUploading = false;
                            }
                }
                
                else if (evt.getSource() == bUpload) {
                ...
                    threadUpload();
                    while(isUploading)
                    {
                        wait(someTime);
                    }
                • 5. Re: Upload multiple files to ftp
                  807606
                  Thanks but this doesn't work i tried it earlier but i tried it again and it stil doesn't work but thx for replying hoping you find another way or something

                  Thx

                  Satanduvel
                  • 6. Re: Upload multiple files to ftp
                    mlk
                    yes
                    and wrapper is an instance of org.apache.commons.net.ftp.FTPClient. I'm fairly sure that is not threadsafe, and that you should create a new client for each thread.
                    • 7. Re: Upload multiple files to ftp
                      807606
                      And how do I have to do that because i don't know how much threads there gonna be needed?

                      Thx 4 reply

                      Satanduvel
                      • 8. Re: Upload multiple files to ftp
                        mlk
                        Create one per thread?
                        • 9. Re: Upload multiple files to ftp
                          807606
                          yes the thread are being called per selected row in the JTable but its always the same thread. So when i pushe upload always threadUpload() will be triggerred so i can't make more clients or can i?

                          In my code per example how or where should i put it in my code?

                          Srry for the misunderstanding from my site but i'm new with threads.

                          At this links there is being said static members are safe but instance not what hava i?
                          http://www.aspose.com/Products/Aspose.Network/Api/Aspose.Network.Ftp.FtpClient.html

                          Satanduvel

                          Message was edited by:
                          Satanduvel
                          • 10. Re: Upload multiple files to ftp
                            mlk
                            At this links there is being said static
                            members are safe but instance not what hava i?
                            Not the same FTP client, not even the same language.

                            Try creating a new FTP client at the start of the method.

                            I'd also look at separating your "business logic" from your GUI code, but that is just me.
                            • 11. Re: Upload multiple files to ftp
                              807606
                              So you mean so? Because this isn't working either
                              public void doUpload(String filename) {
                                        JakartaFtpWrapper wrapper2 = new JakartaFtpWrapper();
                                        try{
                                        wrapper2.connectAndLogin("host", "user", "pass");}
                                        catch (Exception e){}
                                      if (wrapper2.isConnected()){
                                          try{
                                               wrapper2.changeWorkingDirectory(Root);
                                              wrapper2.binary();
                                              lUpload.setText("Uploading file : " + value);
                                               lUpload.setVisible(true);
                                              aProgressBar.setVisible(true);
                                              aProgressBar.setStringPainted(true);
                                              byte[] buffer = new byte[1024];
                                              try {
                                                   String remoteFile = (String) value;
                                                   System.out.println("remotefile:"+remoteFile);
                                                  File f = new File(filename);
                                                  int size = (int) f.length();
                                                  FileInputStream in = new FileInputStream(filename);
                                                  OutputStream out = wrapper2.storeFileStream(remoteFile);
                                                  int counter = 0;
                                                  double bytesUploaded = 0;
                                                  int percVal = 0;
                                                  System.out.println("Root: " + Root);
                                                  while ((counter = in.read(buffer)) >= 0 ) {
                                                      bytesUploaded += counter;
                                                      out.write(buffer,0,counter);
                                                      percVal = (int) ((bytesUploaded / size) * 100); 
                                                      aProgressBar.setValue(percVal);
                                                      aProgressBar.setString("" + percVal + "%");
                                                      if (percVal == 100){
                                                      refreshUpload();
                                                      JOptionPane.showMessageDialog(this,"Uploaden geslaagd","Gelukt",JOptionPane.INFORMATION_MESSAGE);
                                                      lUpload.setVisible(false);
                                                      aProgressBar.setVisible(false);
                                                      }
                                                  }
                                                  out.close();
                                                  in.close();
                                              } catch (Exception ex) {
                                                  JOptionPane.showMessageDialog(this,"Error: " + ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
                                                  lUpload.setVisible(false);
                                                  aProgressBar.setVisible(false);
                                              }
                                          } catch (Exception ex) {
                                              JOptionPane.showMessageDialog(this,"Connectie Error: " + ex.toString(),"ERROR",JOptionPane.ERROR_MESSAGE);
                                          }
                                      } else {
                                           JOptionPane.showMessageDialog(this,"Geen connectie tijdens het uploaden","ERROR",JOptionPane.ERROR_MESSAGE);
                                           connect();
                                           return;
                                      }
                                      postUpload();
                                  }
                              Thx 4 replying

                              Satanduvel
                              • 12. Re: Upload multiple files to ftp
                                mlk
                                catch (Exception e){}
                                Please don't ever do that.
                                ex.toString()
                                Rather than just using the toString, use a logger, and find out which line this is one.
                                • 13. Re: Upload multiple files to ftp
                                  807606
                                  I still get the same error: org.apache.commons.net.MalformedServerReplyException: Truncated server reply.
                                  But the last file thats selected in the JTable is succesfully uploaded but the first one not

                                  Pls come help me if you can.

                                  thx

                                  Satanduvel
                                  • 14. Re: Upload multiple files to ftp
                                    807606
                                    Pls somebody wanne help me because i'm doing this project for my school and my deadline is comming closer.

                                    Many thx

                                    Satanduvel
                                    1 2 Previous Next