簡體   English   中英

java - FTPClient,多線程上傳不起作用。

[英]java - FTPClient, multithread upload doesn't work.

我正在創建一個應用程序,讓您一次上傳最多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); 是。

顯然,您使用相同的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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM