获取MD5程序-图形方式,带进度条

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();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值