importjavax.swing.*;
importjava.awt.*;
importjava.io.*;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.util.Objects;
publicclassGetFilesMd5Value {
publicstaticvoidmain(String[] args) {
JFramejFrame=newJFrame("文件MD5生成及校验");
JLabeljLabel1=newJLabel("1点击按钮选择文件", JLabel.CENTER);
JLabeljLabel2=newJLabel("对比结果显示在这里", JLabel.CENTER);
JTextFieldjtextfield=newJTextField(32);
JButtonjButton1=newJButton("获取MD5");
JButtonjButton2=newJButton("对比MD5");
JFileChooserjFileChooser1=newJFileChooser();
JFileChooserjFileChooser2=newJFileChooser();
Containercontainer=jFrame.getContentPane();
container.setLayout(null);
container.add(jLabel1);
container.add(jLabel2);
container.add(jtextfield);
container.add(jButton1);
container.add(jButton2);
container.add(jFileChooser1);
container.add(jFileChooser2);
jFrame.setSize(515, 250);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
intwindowWidth=jFrame.getWidth(); intwindowHeight=jFrame.getHeight(); Toolkitkit=Toolkit.getDefaultToolkit(); DimensionscreenSize=kit.getScreenSize(); intscreenWidth=screenSize.width; intscreenHeight=screenSize.height; jFrame.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);jtextfield.setHorizontalAlignment(JTextField.CENTER);
jtextfield.setBorder(null);
jtextfield.setBackground(null);
jButton1.setBounds(85, 10, 85, 20);
jButton2.setBounds(335, 10, 85, 20);
jLabel1.setBounds(5, 60, 240, 60);
jtextfield.setBounds(255, 60, 240, 60);
jLabel2.setBounds(130, 135, 240, 60);
jButton1.addActionListener(e-> {
try {
if (Objects.equals(jtextfield.getText(), "")) {
jLabel2.setText("请先获取文件MD5值再点击!");
}
intret=jFileChooser1.showOpenDialog(jButton1.getParent());
if (ret==JFileChooser.APPROVE_OPTION) {
Filefile=jFileChooser1.getSelectedFile();
jLabel1.setText((generateMD5(file.getAbsolutePath())));
}
} catch (HeadlessExceptionheadlessException) {
headlessException.printStackTrace();
}
});
jButton2.addActionListener(e-> {
Strings="";
booleana=jtextfield.getText().equals(s);
System.out.println(a);
if (a) {
jLabel2.setText("请先获取文件MD5值再点击!");
} else {
booleanb=jLabel1.getText().equals(jtextfield.getText());
if (b) {
jLabel2.setText("两个文件为同一文件或该文件并未修改过!");
} else {
jLabel2.setText("两个文件非同一文件或该文件已被修改!");
}
}
});
}
privatestaticbyte[] createChecksum(Stringfilename) {
InputStreamfis=null;
try {
fis=newFileInputStream(filename);
byte[] buffer=newbyte[1024];
MessageDigestcomplete=MessageDigest.getInstance("MD5");
intnumRead;
while ((numRead=fis.read(buffer)) !=-1) {
complete.update(buffer, 0, numRead);
}
returncomplete.digest();
} catch (NoSuchAlgorithmException|IOExceptione) {
e.printStackTrace();
} finally {
try {
if (null!=fis) {
fis.close();
}
} catch (IOExceptione) {
e.printStackTrace();
}
}
returnnull;
}
privatestaticStringgenerateMD5(StringfilePath) {
if (!newFile(filePath).isFile()) {
System.err.println("错误:"+filePath+" 不是有效文件。");
returnnull;
}
byte[] b=createChecksum(filePath);
if (null==b) {
System.err.println(("错误:创建md5字符串失败!"));
returnnull;
}
StringBuilderresult=newStringBuilder();
for (bytevalue : b) {
result.append(Integer.toString((value&0xff) +0x100, 16)
.substring(1));
}
returnresult.toString();
}
}