Reputation: 93
I am making an application that let's you upload up to 5 files at once to server. I am using apache FTPClient and Filezilla server. when I upload one file at a time it works but when I try to get 2 or more uploads going I get socket exception, the first file that was uploading stops and the new one starts. Here's my UploadThread class:
package uploaduptofive;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.apache.commons.net.ftp.FTPClient;
public class ThreadUpload implements Runnable {
FileInputStream inputStream;
long fileLength;
File selFile;
JFrameClass jFrame;
static String fajl;
FTPClient ftpClient;
String ime;
long progress = 0;
int i;
public ThreadUpload(JFrameClass jFrame, FTPClient ftpClient) {
this.jFrame = jFrame;
this.ftpClient = ftpClient;
}
@Override
public synchronized void run() {
i = jFrame.i;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Upload file");
fc.setAcceptAllFileFilterUsed(true);
if (fc.showOpenDialog(jFrame) == JFileChooser.APPROVE_OPTION) {
selFile = fc.getSelectedFile();
System.out.println(selFile.length());
Path path = Paths.get(selFile.toString());
fileLength = selFile.length();
fajl = selFile.toString();
ime = selFile.getName();
System.out.println(ime);
try {
upload();
} catch (FileNotFoundException ex) {
Logger.getLogger(ThreadUpload.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ThreadUpload.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
jFrame.indeksProgress[jFrame.i] = -1;
}
}
protected void upload() throws FileNotFoundException, IOException {
File secondLocalFile = new File(fajl);
String secondRemoteFile = ime;
inputStream = new FileInputStream(secondLocalFile);
OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
byte[] bytesIn = new byte[4096];
int read = 0;
jFrame.nizProgress.get(jFrame.indeksProgress[i]).setValue(0);
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
progress = progress + 4096;
System.out.println("" + ((progress * 100 / fileLength)));
jFrame.nizProgress.get(jFrame.indeksProgress[i]).setValue((int) ((progress * 100 / fileLength)));
}
System.out.println("" + ((((progress) / fileLength) * 100)));
jFrame.nizProgress.get(jFrame.indeksProgress[i]).setValue(100);
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
System.out.println("Uploaded!");
}
}
}
I run threads from class name JFrameClass:
Runnable run = new ThreadUpload(JFrameClass.this, ftpClient);
new Thread(run).start();
here is the error I get:
mar 07, 2013 4:39:01 PM uploaduptofive.ThreadUpload run
SEVERE: null
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at org.apache.commons.net.io.SocketOutputStream.write(SocketOutputStream.java:71)
at uploaduptofive.ThreadUpload.upload(ThreadUpload.java:79)
at uploaduptofive.ThreadUpload.run(ThreadUpload.java:57)
at java.lang.Thread.run(Thread.java:722)
ThreadUpload.java:57 is where upload(); is, and (ThreadUpload.java:79) is where outputStream.write(bytesIn, 0, read); is.
Upvotes: 2
Views: 2806
Reputation: 920
Apparently, you use the same FtpClient for the two upload processes.
You should re-instantiate a new FtpClient to open a new socket and allow several files being downloaded at the same time:
FtpClient ftpClient = new FtpClient() ;
...
//Opens a new socket
ftpClient.connect("ftp.example.com");
...
Runnable run = new ThreadUpload(JFrameClass.this, ftpClient);
new Thread(run).start();
Upvotes: 4