JAVA小小的程序2之文件MD5值校验器V0.1

   上次写字符串MD5值计算器之后没多久写的。现在把代码贴上吧。

  基本和前面的字符串MD5计算器一致,只对其作了小量修改。

代码如下:

界面及按钮相关功能:

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class MainPanel extends JFrame{ File file; private String inString; private String compareString; private String outputString; static JTextField inputText = new JTextField(); static JTextField outputText = new JTextField(); static JTextField compareText = new JTextField(); static JButton fileChoose = new JButton("打开文件"); static JButton compare = new JButton("比较"); static JButton empty = new JButton("清空"); static JButton freeback = new JButton("意见反馈"); // create main user interface MainPanel() { super("MD5校验器 V0.1"); setSize(320, 160); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); NewAction newAction = new NewAction(); JPanel jp = new JPanel(); jp.setLayout(new BorderLayout()); JPanel labels = new JPanel(); labels.setLayout(new GridLayout(3, 1)); labels.add(new JLabel(" 文件选择")); labels.add(new JLabel(" MD5值 ")); labels.add(new JLabel(" 参考值 ")); JPanel texts = new JPanel(); GridLayout textLayout = new GridLayout(3, 1); textLayout.setVgap(5); texts.setLayout(textLayout); inputText.setEditable(false); outputText.setEditable(false); texts.add(inputText); texts.add(outputText); texts.add(compareText); JPanel buttons = new JPanel(); /* FlowLayout buttonsLay = new FlowLayout(); buttonsLay.setHgap(5); buttons.setLayout(buttonsLay); */ fileChoose.addActionListener(newAction); compare.addActionListener(newAction); empty.addActionListener(newAction); freeback.addActionListener(newAction); buttons.add(fileChoose); buttons.add(compare); buttons.add(empty); buttons.add(freeback); jp.add(labels, BorderLayout.WEST); jp.add(texts, BorderLayout.CENTER); jp.add(buttons, BorderLayout.SOUTH); setContentPane(jp); } public static void main(String[] args) { MainPanel panel = new MainPanel(); panel.setVisible(true); } // implements functions of buttons class NewAction implements ActionListener { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JButton button = (JButton) e.getSource(); if ( button == fileChoose) { // 文件选择 JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(fileChoose); if ( JFileChooser.APPROVE_OPTION == returnVal) { file = chooser.getSelectedFile(); inString = file.getName(); inputText.setText(inString); MainAlgorithm md5 = new MainAlgorithm(file); outputString = md5.compute(); outputText.setText(outputString); } } else if (button == compare) { // MD5值比较 if (outputText.getText().equals("")) { JOptionPane.showMessageDialog(null, "请先计算"); } else { // if md5 values are the same if (outputText.getText().equals(compareText.getText())) { JOptionPane.showMessageDialog(null, "MD5值相同"); } else {// md5 values are different JOptionPane.showMessageDialog(null, "MD5值不同"); } } } else if (button == empty) { // 重新初始化 inputText.setText(null); outputText.setText(null); compareText.setText(null); file = null; inString = null; outputString = null; compareString = null; } else { // 反馈问题 try { BareBonesBrowserLaunch.browse("http://blog.sina.com.cn/" + "s/blog_03f9d36e0100qjqp.html"); } catch (IllegalArgumentException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InvocationTargetException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (NoSuchMethodException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } } } /*以下用浏览器打开指定网页的代码采用自互联网,网址没记下来,现在找不到,望原作见谅 */ // open URL with a browser class BareBonesBrowserLaunch { public static void openURL(String url) { try { browse(url); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:/n" + e.getLocalizedMessage()); } } static void browse(String url) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InterruptedException, InvocationTargetException, IOException, NoSuchMethodException { String osName = System.getProperty("os.name", ""); if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else if (osName.startsWith("Windows")) { Runtime.getRuntime().exec( "rundll32 url.dll,FileProtocolHandler " + url); } else { // assume Unix or Linux String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.length && browser == null; count++) if (Runtime.getRuntime() .exec(new String[] { "which", browsers[count] }) .waitFor() == 0) browser = browsers[count]; if (browser == null) throw new NoSuchMethodException("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } } } 计算文件MD5值的实现:

 import java.io.*; import java.security.*; import javax.swing.JOptionPane; public class MainAlgorithm { MessageDigest md5; File file; MainAlgorithm( File inFile) { try { this.file = inFile; md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String compute () { FileInputStream fis = null; try { fis = new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, "Couldn't find the file."); e.printStackTrace(); } byte[] input = new byte[1024]; int hasRead = 0; try { hasRead = fis.read(input); while ( hasRead > 0) { md5.update(input, 0, hasRead); hasRead = fis.read(input); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] result = md5.digest(); StringBuffer valueHex = new StringBuffer(); for ( int i = 0, tmp = 0; i < result.length; i++) { tmp = result[i] & 0xff; if ( tmp < 16) { valueHex.append(0); } valueHex.append(Integer.toHexString(tmp)); } return valueHex.toString(); } }

上一篇:deferred 延迟加载js


下一篇:沙盒机制(sandBox)