import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.security.MessageDigest;
import javax.swing.*;
public class WGetMD5 extends JFrame {
private JFrame frm = this;
private JTextField tf = new JTextField(100);
private JTextField Txt_md5 = new JTextField(30);
private File file = null;
private JLabel title = new JLabel();
private JButton Btn_file = new JButton("选择");
private JButton Btn_calc = new JButton("计算");
private JButton Btn_exit = new JButton("退出");
private JLabel Lab_file = new JLabel("文件名:");
private JProgressBar bar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
public WGetMD5() throws HeadlessException {
frm.setLayout(null);
frm.setTitle("计算MD5");
title.setFont(new Font("楷体_GB2312", Font.BOLD, 30));
title.setForeground(Color.RED);
title.setText("MD5计算程序");
title.setBounds(100, 10, 200, 32);
frm.add(title);
Lab_file.setBounds(10, 50, 50, 22);
frm.add(Lab_file);
tf.setBounds(60, 50, 200, 22);
tf.setEditable(false);
frm.add(tf);
Btn_file.setBounds(280, 50, 70, 22);
Btn_file.addActionListener(new ChooseFile());
frm.add(Btn_file);
bar.setBounds(30, 90, 300, 12);
bar.setVisible(false);
frm.add(bar);
Txt_md5.setBounds(60, 85, 250, 22);
Txt_md5.setBackground(Color.GRAY);
Txt_md5.setForeground(Color.YELLOW);
Txt_md5.setEditable(false);
Txt_md5.setVisible(false);
frm.add(Txt_md5);
Btn_calc.setBounds(80, 120, 70, 22);
Btn_calc.setEnabled(false);
Btn_calc.addActionListener(new CalculateMD5());
frm.add(Btn_calc);
Btn_exit.setBounds(200, 120, 70, 22);
Btn_exit.addActionListener(new ExitPrograme());
frm.add(Btn_exit);
frm.setBounds(100, 100, 400, 200);
frm.setResizable(false);
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frm.setVisible(true);
}
// 以下是各按钮的监听器
class ChooseFile implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser("e:/");
int returnVal = chooser.showOpenDialog(frm);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
tf.setText(file.getAbsolutePath());
Btn_calc.setEnabled(true);
Txt_md5.setVisible(false);
}
}
}
class ExitPrograme implements ActionListener {
public void actionPerformed(ActionEvent e) {
frm.dispose();
System.exit(0);
}
}
class CalculateMD5 implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 启动一个计算MD5的线程
Txt_md5.setVisible(false);
bar.setVisible(true);
new Thread(new GetMD5()).start();
}
}
class GetMD5 implements Runnable {
/**
* @param buf
* 含Md5码的字节数组
* @return MD5码的十六进制表示,存放在字符串中
*/
private String ByteArray2String(byte[] buf) {
char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
// MD5码由16个字节,128位组成,转换成32个字符
char[] md5 = new char[32];
int k = 0;
for (byte b : buf) {
md5[k++] = hexChar[b >>> 4 & 0xf];
md5[k++] = hexChar[b & 0xf];
}
String md5String = new String(md5);
return md5String;
}
public void run() {
String res = null;
long length = file.length();
long cur_len = 0, old_len = 0, step = 1;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file));
byte[] b = new byte[4096];
int c = 0;
c = in.read(b);
while (c >= 0) {
cur_len += c;
if (c == b.length)
md.update(b);
else
md.update(b, 0, c);
if ((cur_len - old_len) * 100 / length > step) {
bar.setValue((int) (cur_len * 100 / length));
old_len = cur_len;
}
c = in.read(b);
}
in.close();
res = ByteArray2String(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
bar.setVisible(false);
Txt_md5.setText(res);
Txt_md5.setVisible(true);
}
}
public static void main(String[] args) {
new WGetMD5();
}
}
获取MD5程序-图形方式,带进度条
最新推荐文章于 2019-05-20 14:23:48 发布