Java值必须介于0和100之间

Java值必须介于0和100之间,java,swing,file-io,swingworker,jprogressbar,Java,Swing,File Io,Swingworker,Jprogressbar,我有以下代码: 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

我有以下代码:

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;
}
}
package com.cjburkey.downloads.wie_ein_chef;
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.beans.PropertyChangeEvent;
导入java.beans.PropertyChangeListener;
导入java.io.BufferedOutputStream;
导入java.io.File;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.concurrent.ExecutionException;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JProgressBar;
导入javax.swing.SwingWorker;
导入net.lingala.zip4j.core.ZipFile;
导入net.lingala.zip4j.exception.zipeexception;
公共类下载{
最终静态JFrame frm=新JFrame();
最终静态JLabel状态=新JLabel(“下载…”);
最终静态按钮取消=新按钮(“取消”);
静态文件fileDown=null;
静态字符串=null;
公共下载(字符串站点、文件文件、字符串目录){
fileDown=文件;
dire=dir;
最终JProgressBar电流=新JProgressBar(0,100);
当前设置大小(50100);
当前设置值(0);
当前设置字符串已绘制(真);
frm.设置尺寸(640480);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(新的FlowLayout());
frm.add(状态);
frm.添加(当前);
增加(取消)财务报表;
frm.pack();
frm.setLocationRelativeTo(空);
frm.可设置大小(假);
frm.setVisible(真);
最终工人=新工人(现场、文件);
worker.addPropertyChangeListener(新的PropertyChangeListener(){
@凌驾
公共作废属性更改(属性更改事件pcEvt){
if(“progress”.equals(pcEvt.getPropertyName())){
current.setValue((整数)pcEvt.getNewValue());
}else if(pcEvt.getNewValue()==SwingWorker.StateValue.DONE){
试一试{
worker.get();
}捕获(中断异常|执行异常e){
showMessageDialog(frm,e.getMessage(),null,JOptionPane.ERROR_MESSAGE);
frm.getContentPane().removeAll();
frm.dispose();
Home.frame.getContentPane().removeAll();
Home.frame.repaint();
新家();
}
}
}
});
cancel.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
int option=JOptionPane.showConfirmDialog(frm,“您确定要取消下载吗?”);
if(option==JOptionPane.YES\u选项){
Download.fileDown.delete();
系统出口(0);
}否则{
showMessageDialog(frm,“恢复”);
}
}
});
worker.execute();
}
}
类Worker扩展了SwingWorker{
私人网站;
私有文件;
公共工作者(字符串站点、文件){
this.site=站点;
this.file=文件;
}
@凌驾
受保护的Void doInBackground()引发异常{
URL=新URL(站点);
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=新的java.io.FileOutputStream(文件);
try(java.io.BufferedOutputStream bout=newbufferedoutputstream(fos,1024)){
字节[]数据=新字节[1024];
int i;
而((i=in.read(data,01024))>=0){
totalDataRead=totalDataRead+i;
写(数据,0,i);
整数百分比=(totalDataRead*100)/文件大小;
Download.status.setText(totalDataRead/1024+“kb/”+filesize/1024+“kb”);
进度(百分比);
Download.frm.setTitle(“下载文件”);
下载.frm.pack();
下载.frm.setLocationRelativeTo(空);
如果(百分比==100.0){
jbuttonfinish=newjbutton(“完成安装(警告:将覆盖您当前的MINECRAFT配置文件)”);
下载.frm.getContentPane().removeAll();
下载.frm.repaint();
下载.frm.add(完成);
下载.frm.setTitle(“提取…”);
下载.frm.pack();
下载.frm.setLocationRelativeTo(空);
finish.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
Download.status.setText(“等待”);
ZipFile文件=null;
试一试{
file=新ZipFile(Download.fileDown);
}捕获(ZipException e1){
showMessageDialog(null,e1.getMessage(),null,JOptionPane.ERROR_MESSAGE);
}
System.out.println(Download.fileDown.toString());
setRunInThread文件(true);
试一试{
file.extractAll(下载.dire);
showMessageDialog(null,“已完成安装!!”);
Download.fileDown.delete();
系统出口(0);
}捕获(ZipException e1){
showMessageDialog(null,e1.getMessage(),null,JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
}
}
返回null;
}
}
它过去工作得很好,但现在,当它达到27%左右时,就会产生一个误差
int percent = (totalDataRead * 100) / filesize;
Download.status.setText(totalDataRead / 1024 + "kb/" + filesize / 1024 + "kb");
setProgress(percent);