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

简介:    上次写字符串MD5值计算器之后没多久写的。现在把代码贴上吧。   基本和前面的字符串MD5计算器一致,只对其作了小量修改。代码如下:界面及按钮相关功能: import javax.

   上次写字符串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(); } }

目录
相关文章
|
4天前
|
Java
【Java开发指南 | 第二十一篇】Java流之文件
【Java开发指南 | 第二十一篇】Java流之文件
13 0
|
3天前
|
消息中间件 Java Kafka
Java大文件排序(有手就能学会),kafka面试题2024
Java大文件排序(有手就能学会),kafka面试题2024
|
3天前
|
Java 关系型数据库 MySQL
MySql数据库级别MD5加密java MD5加密解密工具包
MySql数据库级别MD5加密java MD5加密解密工具包
|
4天前
|
安全 Java 开发者
Java一分钟之-文件与目录操作:Path与Files类
【5月更文挑战第13天】Java 7 引入`java.nio.file`包,`Path`和`Files`类提供文件和目录操作。`Path`表示路径,不可变。`Files`包含静态方法,支持创建、删除、读写文件和目录。常见问题包括:忽略异常处理、路径解析错误和权限问题。在使用时,注意异常处理、正确格式化路径和考虑权限,以保证代码稳定和安全。结合具体需求,这些方法将使文件操作更高效。
11 2
|
4天前
|
前端开发 Java 应用服务中间件
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
17 0
|
4天前
|
Java 开发者
Java一分钟之-Java IO流:文件读写基础
【5月更文挑战第10天】本文介绍了Java IO流在文件读写中的应用,包括`FileInputStream`和`FileOutputStream`用于字节流操作,`BufferedReader`和`PrintWriter`用于字符流。通过代码示例展示了如何读取和写入文件,强调了常见问题如未关闭流、文件路径、编码、权限和异常处理,并提供了追加写入与读取的示例。理解这些基础知识和注意事项能帮助开发者编写更可靠的程序。
17 0
|
4天前
|
Java Linux C语言
一步带你了解java程序逻辑控制
一步带你了解java程序逻辑控制
17 2
|
4天前
|
Java 数据安全/隐私保护
java中程序控制的典例
java中程序控制的典例
13 1
|
4天前
|
Java
JDK环境下利用记事本对java文件进行运行编译
JDK环境下利用记事本对java文件进行运行编译
16 0
|
4天前
|
存储 Java 数据库连接
使用Java开发桌面应用程序
使用Java开发桌面应用程序
21 0