我有一个简单的 GUI 程序,它与数据库进行一些交互,然后在完成后将一些文件上传到服务器。在我添加 SwingWorker 线程以保持 GUI 响应之前,这一直工作得很好。
如果我将其放入其独立项目中,我使用的代码可以正常工作,但在该项目(SwingWorker 线程)内,它会出现以下错误:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at sun.nio.cs.StreamDecoder.read0(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openActiveDataTransferChannel(FTPClient.java:3511)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3475)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2641)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2550)
at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2421)
at GUI$MatchFiles.doInBackground(GUI.java:1602)
at GUI$MatchFiles.doInBackground(GUI.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
有什么想法吗?
编辑: 我想你的意思是这样的?请注意,这是一个非常精简的版本,我删除了很多多余的代码。我看不出有任何影响。
class Worker extends SwingWorker<Integer, Integer>{
protected Integer doInBackground() throws Exception{
FTPClient client = new FTPClient();
client.connect(url);
client.login(username, password);
client.setPassive(false);
client.changeDirectory(uploaddirectory);
client.upload(new File(fileuploadpath));
client.disconnect(true);
}
protected void done() {
System.out.println("Done");
}
}
如前所述,我将这段代码放入 swingworker 中,并将其放入自己的类中,并且运行完美。
最佳答案
看起来您的套接字被防火墙或防病毒软件切断了...尝试禁用防火墙和防病毒软件并重新运行您的程序...
关于Java FTP 无法在 SwingWorker 线程内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10443474/