2

我正在制作一个应用程序,让您一次最多将 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); 是。

4

1 回答 1

4

显然,您对两个上传过程使用相同的 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();
于 2013-03-07T16:38:06.400 回答