我正在创建一个应用程序,让您一次上传最多5个文件到服务器。我正在使用apache FTPClient和Filezilla服务器。当我一次上传一个文件时,它可以工作,但当我尝试上传2个或更多时,我得到套接字异常,上传的第一个文件停止,新的文件启动。这是我的UploadThread类:
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!");
}
}
}
我从类名JFrameClass运行线程:
Runnable run = new ThreadUpload(JFrameClass.this, ftpClient);
new Thread(run).start();
这是我得到的错误:
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是upload();是,和(ThreadUpload.java:79)是outputStream.write(bytesIn,0,read);是。
答案 0 :(得分:3)
显然,您使用相同的FtpClient进行两个上传过程。
您应该重新实例化一个新的FtpClient以打开一个新的套接字并允许同时下载多个文件:
FtpClient ftpClient = new FtpClient() ;
...
//Opens a new socket
ftpClient.connect("ftp.example.com");
...
Runnable run = new ThreadUpload(JFrameClass.this, ftpClient);
new Thread(run).start();