gpt4 book ai didi

java - FTPClient,多线程上传不起作用。

转载 作者:行者123 更新时间:2023-11-29 05:47:33 27 4
gpt4 key购买 nike

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

关于java - FTPClient,多线程上传不起作用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15275954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com