gpt4 book ai didi

java - ProgressMonitor 和 SwingWorker 不工作

转载 作者:行者123 更新时间:2023-12-01 13:00:34 25 4
gpt4 key购买 nike

我在使用 SwingWorker 时遇到问题。应用程序在客户端和服务器之间发送文件,但进度监视器不会在传输过程中向我显示进度。你能告诉我我做错了什么以及我应该做什么吗?

两个应用程序的主类是相同的:

package main;

import java.awt.EventQueue;

public class Main {

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {

Test2 test2=new Test2();

}
});

}

}

客户端:

package main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class Test2 {

public Test2() {

hostName="localhost";

try {
clientSocket = new Socket(hostName, 1234);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

File file=new File("E:/test/123.mp4");

try {
fileInputStream=new FileInputStream(file);
bis=new BufferedInputStream(fileInputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
bos=new BufferedOutputStream(clientSocket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

bit=new byte[512];
int len;

System.out.println("Send..."); //test

try {
while ((len = bis.read(bit,0,511)) != -1) {
bos.write(bit, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
bis.close();
bos.close();
fileInputStream.close();
//fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Finish"); //test

}

private String hostName;
private Socket clientSocket;

private BufferedInputStream bis;
private BufferedOutputStream bos;
private FileInputStream fileInputStream;
private byte bit[];
}

服务器:

package main;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

public class Test2 {

public Test2() {

pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
pm.setMillisToDecideToPopup(1);

test4=new Test4();
test4.execute();

}

private class Test4 extends SwingWorker<Boolean, Void> {

public Test4() {

try {
welcomeSocket = new ServerSocket(1234);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Server works");

try {
connectionSocket = welcomeSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
protected Boolean doInBackground() throws Exception {

try {

bis=new BufferedInputStream(connectionSocket.getInputStream());

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
bos=new BufferedOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

bit=new byte[512];
int len;

System.out.println("Download..."); //test

try {
while ((len = bis.read(bit,0,511)) != -1) {

bos.write(bit, 0, len);

publish();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
bis.close();
bos.close();
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return true;

}

@Override
protected void process(List<Void> chunks) {

number++;
pm.setProgress(number);

}

@Override
protected void done() {
System.out.println("DONE");
}

}

private ServerSocket welcomeSocket;
private Socket connectionSocket;

private FileOutputStream fileOutputStream;
private BufferedInputStream bis;
private BufferedOutputStream bos;
private byte bit[];

private ProgressMonitor pm;
private Test4 test4;
private int number;

}

除了 SwingWorker 之外,我还遇到了以下代码的问题。这是我的服务器应用程序的第二个版本,没有 SwingWorker。在这里,进度监视器向我显示传输过程中的进度,但不是永远。我在 run() 方法中使用了 invokeLater 但有时进度监视器不起作用。你能告诉我我做错了什么吗?

服务器:

package main;

import java.awt.EventQueue;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

public class Test2 {

public Test2() {

pm=new ProgressMonitor(null, "Download...", null, 0, 1850297);
pm.setMillisToDecideToPopup(1);

test3=new Test3();
new Thread(test3).start();

}

private class Test3 implements Runnable {

public Test3() {

}

@Override
public void run() {

try {
welcomeSocket = new ServerSocket(1234);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("Server works"); //test

while(true){

try {
connectionSocket = welcomeSocket.accept();

try {

bis=new BufferedInputStream(connectionSocket.getInputStream());

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
fileOutputStream=new FileOutputStream("E:/test2/123.mp4");
bos=new BufferedOutputStream(fileOutputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

bit=new byte[512];
int len;

System.out.println("Download..."); //test

try {
while ((len = bis.read(bit,0,511)) != -1) {

bos.write(bit, 0, len);

EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
number++;
pm.setProgress(number);

}
});

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
bis.close();
bos.close();
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("DONE"); //test

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

}

private ServerSocket welcomeSocket;
private Socket connectionSocket;

private FileOutputStream fileOutputStream;
private BufferedInputStream bis;
private BufferedOutputStream bos;
private byte bit[];

private ProgressMonitor pm;
private Test3 test3;
private int number;

}

最佳答案

您正在等待套接字连接并在 Test4 构造函数中接受它,该构造函数将在 Swing 事件线程上调用。可以在那里创建套接字,但不要等待连接,因为这会阻塞。而是在 doInBackground 方法中等待连接。

还要考虑更改您的 SwingWorker,以便它传递读取的字节:

// change generic parameter to Integer
private class Test4 extends SwingWorker<Boolean, Integer> {


@Override
protected Boolean doInBackground() throws Exception {

// .....

bit=new byte[512];
int len;

try {
while ((len = bis.read(bit,0,511)) != -1) {

bos.write(bit, 0, len);

publish(len);

}

以及其他地方:

    @Override
protected void process(List<Integer> chunks) {
// number++;
for (Integer chunk : chunks) {
pm.setProgress(chunk);
}
}

关于java - ProgressMonitor 和 SwingWorker 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23546141/

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