我有一个套接字连接的客户端和服务器。我尝试对服务器实施progressMonitorInputStream(),数据传输完美,但是进度监视器不可见。我尝试实现它以便仅从一侧读取文件,因此进度监视器工作正常。以下是两个代码
服务器:
public class Server implements ActionListener, PropertyChangeListener {
DataInputStream dis;
ProgressMonitor pm;
ProgressMonitorInputStream pmis;
JFrame frm;
Server s;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Server().createAndShowUI();
}
});
}
private void createAndShowUI() {
frm = new JFrame();
JPanel panel = new JPanel();
JButton st = new JButton("Start");
st.setPreferredSize(new Dimension(80, 25));
panel.add(st);
panel.setSize(500, 50);
frm.add(panel);
frm.setLocationRelativeTo(null);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.pack();
frm.show();
st.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
frm.dispose();
try {
ServerSocket ss = new ServerSocket(5555);
Socket soc = ss.accept();
dis = new DataInputStream(soc.getInputStream());
pmis = new ProgressMonitorInputStream(frm, "Progress", dis);
pm = pmis.getProgressMonitor();
// pm.setMillisToPopup(1);
Task task = new Task();
task.addPropertyChangeListener(s);
task.execute();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int progress = (Integer) evt.getNewValue();
pm.setProgress(progress);
}
}
class Task extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
int progress = 0;
setProgress(0);
int x;
while ((x = pmis.read()) != -1) {
System.out.print((char) x);
setProgress(progress);
progress++;
}
pmis.close();
return null;
}
@Override
public void done() {
pm.close();
}
}
}
上面的代码是有问题的。工作代码如下:
一侧带有文件输入流的正确工作代码
public class ProgressMonitorOwn implements ActionListener,
PropertyChangeListener {
JFrame frm;
Task task;
DataInputStream dis;
ProgressMonitorInputStream pmis;
JButton but;
ProgressMonitor pm;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ProgressMonitorOwn().createAndShowUI();
}
});
}
private void createAndShowUI() {
frm = new JFrame();
JPanel panel = new JPanel();
but = new JButton("Show");
but.setPreferredSize(new Dimension(100, 25));
panel.add(but);
panel.setSize(200, 100);
frm.add(panel);
frm.setSize(200, 100);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLocationRelativeTo(null);
frm.show();
but.addActionListener(this);
}
class Task extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
int x;
int prog = 0;
while ((x = pmis.read()) != -1) {
System.out.print((char) x);
setProgress(prog);
Thread.sleep(100);
prog++;
}
return null;
}
@Override
public void done() {
but.setEnabled(true);
pm.close();
System.exit(0);
}
}
@Override
public void actionPerformed(ActionEvent e) {
but.setEnabled(false);
try {
FileInputStream fis = new FileInputStream("D:\\test\\test.txt");
dis = new DataInputStream(fis);
pmis = new ProgressMonitorInputStream(frm, "ProgressTest", dis);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
task = new Task();
task.addPropertyChangeListener(this);
task.execute();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress" .equals(evt.getPropertyName())) {
int progress = (Integer) evt.getNewValue();
pm = pmis.getProgressMonitor();
pm.setProgress(progress);
}
}
}
我无法弄清楚为什么“进度监视器”显示在FileInputStream程序中,而不显示在套接字程序上。有任何想法吗?
最佳答案
您需要在setMaximum()
的ProgressMonitor
上调用ProgressMonitorInputStream
达到预期的下载大小。否则,它不知道“进度”是什么意思。
您不必使用setProgress()
调用ProgressMonitorInputStream
。它会自动执行。
您不需要DataInputStream.
您一次应该读一个字节以上。读入byte[]
缓冲区。