It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Upload multiple files to ftp

Upload multiple files to ftp

Postby Satanduvel on Thu Apr 19, 2007 11:24 am

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:

Code: Select all
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

Code: Select all
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:

Code: Select all
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
Satanduvel
 
Posts: 8
Joined: Thu Apr 19, 2007 11:20 am

Postby ggomez on Fri Apr 20, 2007 5:38 am

public void doUpload(String filename)

this must be syncronized like:

public syncronized void doUpload(String filename)

good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby Satanduvel on Fri Apr 20, 2007 6:48 am

Hey thank you very much i don't get a error any more. But now i have another problem. When i select 2 or 3 rows only the last file wil be uploaded (2or3times). So the program knows how much rows are selected but takes the last row en upload that file 2 or 3 times. How can i solve this problem?

Many thx

Satanduvel
Satanduvel
 
Posts: 8
Joined: Thu Apr 19, 2007 11:20 am

Postby ggomez on Sun Apr 22, 2007 6:28 am

i assume that you are using listselectionlistener

with that you can get the index of the data,

then you call the object with table.valueAt(index);

if with this you still have that problem maybe you are not handling the selected items correctly.

post some code about the jtable part if you want more help.

good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby Satanduvel on Mon Apr 23, 2007 6:38 am

No I get the selected values of the Jtable when a button is clicked. And then I get with this code:
Code: Select all
TableModel tm = table.getModel();
                            value = tm.getValueAt(selRows[a],i);

the name of the file. And then threadUpload is being triggered.

This is the "hole" code:

Code: Select all
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();
                         }
                      }
                   }
               }
           } catch (Exception e){}
        }
Satanduvel
 
Posts: 8
Joined: Thu Apr 19, 2007 11:20 am

Postby ggomez on Mon Apr 23, 2007 7:15 am

mmmm ok,

i can see that you are not collecting the values that you get.

you are updating tha "value" variable but at the end you execute the thread, and the thread call the variable "value" but that variable can take one value.

Put the selected items in a vector or something like that.


good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby Satanduvel on Mon Apr 23, 2007 7:49 am

the uploading of more than 1 file is working i was sending a wrong value somewhere :? .
Now i'm gonna begin to the downloading of more than 1 file hope to have no problems any more :)

Satanduvel
Satanduvel
 
Posts: 8
Joined: Thu Apr 19, 2007 11:20 am

Postby ggomez on Mon Apr 23, 2007 8:07 am

Ok then

good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡


Who is online

Users browsing this forum: No registered users and 1 guest