import javax.print.DocFlavor.URL;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.awt.*;
public class FileTest implements ActionListener{
JFrame f;
TextArea ta;
JFileChooser fc;
Container c;
File myFile;
JButton jb1=new JButton("生成视频文件hash值");
JButton jb2=new JButton("保存hash文件列表到videohash.txt");
JPanel jpl=new JPanel();
String hash="";
public static void main(String args[]){
FileTest demo=new FileTest();
demo.go();
}
void go(){
JFrame f=new JFrame("获得视频文件的Hash ");
ta=new TextArea("",10,30,TextArea.SCROLLBARS_BOTH);
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null,"弹出对话框");
fc=new JFileChooser();
java.net.URL dir=FileTest.class.getProtectionDomain().getCodeSource().getLocation();
String path=dir.getPath()+"VVDown";
fc.setCurrentDirectory(new File(path));
int selected=fc.showOpenDialog(c);
if (selected==JFileChooser.APPROVE_OPTION){
myFile=fc.getSelectedFile();
String VideoInfo;
try {
VideoInfo = fileMD5(myFile.getAbsolutePath());
hash+=myFile.getAbsolutePath()+" "+VideoInfo+"\r\n";
ta.setText(hash);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
jb2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
java.net.URL dir=FileTest.class.getProtectionDomain().getCodeSource().getLocation();
String path=dir.getPath()+"VVDown/videohash.txt";
String text = ta.getText();
String[] lines = text.split("\n");
FileWriter fw;
try {
fw = new FileWriter(path);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < lines.length; i++) {
bw.write(lines[i] + "\r\n");
}
bw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"保存");
}
});
c=f.getContentPane();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jpl.add(jb1);
jpl.add(jb2);
f.getContentPane().add("North",jpl);
f.getContentPane().add("Center",ta);
f.setLocation(300, 200);
f.setSize(800,600);
f.setVisible(true);
}
public static String byteArrayToHex(byte[] byteArray) {
// 首先初始化一个字符数组,用来存放每个16进制字符
char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f' };
// new一个字符数组,这个就是用来组成结果字符串的(解释一下:一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方))
char[] resultCharArray =new char[byteArray.length * 2];
// 遍历字节数组,通过位运算(位运算效率高),转换成字符放到字符数组中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b& 0xf];
}
// 字符数组组合成字符串返回
return new String(resultCharArray);
}
public String fileMD5(String inputFile) throws IOException {
// 缓冲区大小(这个可以抽出一个参数)
int bufferSize = 256 * 1024;
FileInputStream fileInputStream = null;
DigestInputStream digestInputStream = null;
try {
// 拿到一个MD5转换器(同样,这里可以换成SHA1)
MessageDigest messageDigest =MessageDigest.getInstance("MD5");
// 使用DigestInputStream
fileInputStream = new FileInputStream(inputFile);
digestInputStream = new DigestInputStream(fileInputStream,messageDigest);
// read的过程中进行MD5处理,直到读完文件
byte[] buffer =new byte[bufferSize];
while (digestInputStream.read(buffer) > 0);
// 获取最终的MessageDigest
messageDigest= digestInputStream.getMessageDigest();
// 拿到结果,也是字节数组,包含16个元素
byte[] resultByteArray = messageDigest.digest();
// 同样,把字节数组转换成字符串
return byteArrayToHex(resultByteArray);
} catch (NoSuchAlgorithmException e) {
return null;
} finally {
try {
digestInputStream.close();
} catch (Exception e) {
}
try {
fileInputStream.close();
} catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e){
}
}