代码比较简单,不做解释
主界面调用方法,具体使用场景,需要根据自己的需求去封装
package com.demo;
import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import thread.FileCodeListener;
import thread.FileCodeRunnable;
import thread.FileUnCodeRunnable;
import thread.FileUtil;
public class TestDemo {
static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
// codeFile();
unCodeFile();
}
/**
* 文件解密方法
*/
private static void unCodeFile() {
String Path = "G:\\filmVideo\\417\\american";
List<File> lists = FileUtil.getFileListFromPath(Path);
if (lists == null || lists.size() < 1) {
System.out.println("====文件夹的个数 0 ==");
return;
}
System.out.println("====文件夹的个数==" + lists.size());
for (int i = 0; i < lists.size(); i++) {
String fileName = lists.get(i).getName();
String filePath = lists.get(i).getAbsolutePath();
String cachePath = "G:\\filmVideo\\417\\americanCache\\" + fileName;
FileUnCodeRunnable runnbale = new FileUnCodeRunnable(filePath, cachePath, new FileCodeListener() {
@Override
public void codeFileStatues(boolean isSuccess, String desc) {
System.out.println("====codeFileStatues====" + isSuccess + " / " + desc);
}
@Override
public void codeFileProgress(int progress) {
System.out.println("====progress====" + progress);
}
});
executor.execute(runnbale);
}
}
/**
* 文件加密
*/
private static void codeFile() {
String filePath = "E:\\log\\aaa.mp4";
String cachePath = "E:\\log\\bbb.mp4";
FileCodeRunnable runnbale = new FileCodeRunnable(cachePath, filePath, new FileCodeListener() {
@Override
public void codeFileStatues(boolean isSuccess, String desc) {
System.out.println("====codeFileStatues====" + isSuccess + " / " + desc);
}
@Override
public void codeFileProgress(int progress) {
System.out.println("====progress====" + progress);
}
});
executor.execute(runnbale);
}
}
package thread;
public class Config {
public static final String PASSWORD = "123456";
}
转码回调类
package thread;
public interface FileCodeListener {
// 转码进度
void codeFileProgress(int progress);
// 转码状态
void codeFileStatues(boolean isSuccess, String desc);
}
package thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
/**
* 文件加密方法
*
* @author Administrator
*
*/
public class FileCodeRunnable implements Runnable {
String filePath;
String cachePath;
FileCodeListener listener;
long fileLenth = 1;
public FileCodeRunnable(String filePath, String cachePath, FileCodeListener listener) {
this.filePath = filePath;
this.cachePath = cachePath;
this.listener = listener;
}
@Override
public void run() {
File file = new File(filePath);
if (!file.exists()) {
listener.codeFileStatues(false, "文件不存在");
return;
}
fileLenth = file.length();
enCode();
}
public void enCode() {
InputStream in = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
in = new FileInputStream(filePath);
out = new FileOutputStream(cachePath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
int readLength;
long sumLength = 0;
while ((readLength = in.read(buffer)) > 0) {
for (int i = 0; i < readLength; i++) {
byte b = buffer[i];
buffer2[i] = b == 255 ? 0 : ++b;// 文件的每一byte增1
}
sumLength = sumLength + readLength;
int progress = (int) (sumLength * 100 / fileLenth);
listener.codeFileProgress(progress);
out.write(buffer2, 0, readLength);
out.flush();
}
appendTail(cachePath, Config.PASSWORD);
listener.codeFileStatues(true, "转码完成");
} catch (Exception e) {
listener.codeFileStatues(true, "转码异常:" + e.toString());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (Exception e2) {
}
}
}
/**
* 在文件尾添加指定的字符串
*
* @param fileName
* @param content
*/
private static void appendTail(String fileName, String content) {
try {
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解密工具类
package thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件解密方法
*
* @author Administrator
*
*/
public class FileUnCodeRunnable implements Runnable {
String filePath;
String cachePath;
FileCodeListener listener;
long fileLenth = 1;
public FileUnCodeRunnable(String filePath, String cachePath, FileCodeListener listener) {
this.filePath = filePath;
this.cachePath = cachePath;
this.listener = listener;
}
@Override
public void run() {
File file = new File(filePath);
if (!file.exists()) {
listener.codeFileStatues(false, "文件不存在");
return;
}
fileLenth = file.length();
unCode();
}
public void unCode() {
InputStream is = null;
OutputStream out = null;
try {
File file = new File(filePath);
if (!file.exists()) {
return;
}
File dest = new File(cachePath);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
is = new FileInputStream(filePath);
out = new FileOutputStream(cachePath);
byte[] buffer = new byte[1024];
byte[] buffer2 = new byte[1024];
byte bMax = (byte) 255;
long size = file.length() - Config.PASSWORD.length();
int mod = (int) (size % 1024);
int div = (int) (size >> 10);
int count = mod == 0 ? div : (div + 1);
int k = 1;
int readLength = 0;
long sumWrite = 0; // 已经读写的
while ((k <= count && (readLength = is.read(buffer)) > 0)) {
if (mod != 0 && k == count) {
readLength = mod;
}
for (int i = 0; i < readLength; i++) {
byte b = buffer[i];
buffer2[i] = b == 0 ? bMax : --b;// 文件的每一个byte减1
}
sumWrite = sumWrite + readLength;
int progress = (int) (sumWrite * 100 / fileLenth);
listener.codeFileProgress(progress);
out.write(buffer2, 0, readLength);
k++;
}
listener.codeFileStatues(true, "解码成功");
} catch (Exception e) {
listener.codeFileStatues(false, "解码异常:" + e.toString());
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (is != null) {
is.close();
}
} catch (Exception e2) {
}
}
}
}