我有这段代码:
package com.cjburkey.downloads.wie_ein_chef;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutionException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class Download {
final static JFrame frm = new JFrame();
final static JLabel status = new JLabel("Downloading...");
final static JButton cancel = new JButton("Cancel");
static File fileDown = null;
static String dire = null;
public Download(String site, File file, String dir) {
fileDown = file;
dire = dir;
final JProgressBar current = new JProgressBar(0, 100);
current.setSize(50, 100);
current.setValue(0);
current.setStringPainted(true);
frm.setSize(640, 480);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new FlowLayout());
frm.add(status);
frm.add(current);
frm.add(cancel);
frm.pack();
frm.setLocationRelativeTo(null);
frm.setResizable(false);
frm.setVisible(true);
final Worker worker = new Worker(site, file);
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if ("progress".equals(pcEvt.getPropertyName())) {
current.setValue((Integer) pcEvt.getNewValue());
} else if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
try {
worker.get();
} catch (InterruptedException | ExecutionException e) {
JOptionPane.showMessageDialog(frm, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
frm.getContentPane().removeAll();
frm.dispose();
Home.frame.getContentPane().removeAll();
Home.frame.repaint();
new Home();
}
}
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(frm, "Are you sure you wish to cancel download?");
if(option == JOptionPane.YES_OPTION) {
Download.fileDown.delete();
System.exit(0);
} else {
JOptionPane.showMessageDialog(frm, "Resuming");
}
}
});
worker.execute();
}
}
class Worker extends SwingWorker<Void, Void> {
private String site;
private File file;
public Worker(String site, File file) {
this.site = site;
this.file = file;
}
@Override
protected Void doInBackground() throws Exception {
URL url = new URL(site);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
int totalDataRead = 0;
try (java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream())) {
java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
try (java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024)) {
byte[] data = new byte[1024];
int i;
while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
int percent = (totalDataRead * 100) / filesize;
Download.status.setText(totalDataRead / 1024 + "kb/" + filesize / 1024 + "kb");
setProgress(percent);
Download.frm.setTitle("Downloading File");
Download.frm.pack();
Download.frm.setLocationRelativeTo(null);
if(percent == 100.0) {
JButton finish = new JButton("Finish Install(WARNING: WILL OVERRIDE YOU CURRENT MINECRAFT PROFILES)");
Download.frm.getContentPane().removeAll();
Download.frm.repaint();
Download.frm.add(finish);
Download.frm.setTitle("Extracting...");
Download.frm.pack();
Download.frm.setLocationRelativeTo(null);
finish.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Download.status.setText("Waiting");
ZipFile file = null;
try {
file = new ZipFile(Download.fileDown);
} catch (ZipException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), null, JOptionPane.ERROR_MESSAGE);
}
System.out.println(Download.fileDown.toString());
file.setRunInThread(true);
try {
file.extractAll(Download.dire);
JOptionPane.showMessageDialog(null, "Completed Install!!");
Download.fileDown.delete();
System.exit(0);
} catch (ZipException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), null, JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
}
}
return null;
}
}
它过去工作正常,但是现在,当它达到约27%时,它会出错,抱怨价值必须介于0到100之间,但问题是,它用于工作,突然间,它没有。为什么呢?
答案 0 :(得分:1)
您遇到的问题是这段代码:
int percent = (totalDataRead * 100) / filesize;
Download.status.setText(totalDataRead / 1024 + "kb/" + filesize / 1024 + "kb");
setProgress(percent);
由于异常消息来自setProgress方法,因此百分比值必须在0 ... 100范围之外: http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html#setProgress(int)
注意:
IllegalArgumentException - 值不是0到100