Excel上传,加密,解析
前言
我们开发的时候会遇到上传excel文件并解析,然后存到数据库;同时呢,我们也希望这份文件加密保存到服务器,以供下载。
以下是本篇文章正文内容,下面案例可供参考
一、Excel文件上传
可参考下这:文件上传
二、文件加密
public class FileEncryptUtil {
private static final String fileFolder = "/vgc_data_lake/dwh/encrypt_files";
private static final String filePath = "/vgc_data_lake/dwh/encrypt_files/";
/**
* AES密码加密私钥(Base64加密)
*/
private static String encryptAESKey;
@Value("${encryptAESKey}")//配置文件中配置
public void setEncryptAESKey(String encryptAESKey) {
FileEncryptUtil.encryptAESKey = encryptAESKey;
}
/**
* 加密
*
* @param arr
* @return java.lang.String
*/
public static byte[] enCrypto(byte[] arr) throws Exception {
Security.addProvider(new SunJCE());
// 实例化支持AES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
// KeyGenerator 提供对称密钥生成器的功能,支持各种算法
KeyGenerator keygen = KeyGenerator.getInstance("AES");
// 将私钥encryptAESKey先Base64解密后转换为byte[]数组按128位初始化
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(Base64ConvertUtil.decode(encryptAESKey).getBytes());
keygen.init(128, secureRandom);
// SecretKey 负责保存对称密钥 生成密钥
SecretKey desKey = keygen.generateKey();
// 生成Cipher对象,指定其支持的AES算法,Cipher负责完成加密或解密工作
Cipher c = Cipher.getInstance("AES");
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, desKey);
byte[] src = arr;
// 该字节数组负责保存加密的结果
byte[] cipherByte = c.doFinal(src);
// 先将二进制转换成16进制,再返回Base64加密后的String
return cipherByte;
}
/**
* 解密
*
* @param arr
* @return java.lang.String
*/
public static byte[] deCrypto(byte[] arr) throws Exception {
Security.addProvider(new SunJCE());
// 实例化支持AES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
// KeyGenerator 提供对称密钥生成器的功能,支持各种算法
KeyGenerator keygen = KeyGenerator.getInstance("AES");
// 将私钥encryptAESKey先Base64解密后转换为byte[]数组按128位初始化
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(Base64ConvertUtil.decode(encryptAESKey).getBytes());
keygen.init(128, secureRandom);
// SecretKey 负责保存对称密钥 生成密钥
SecretKey desKey = keygen.generateKey();
// 生成Cipher对象,指定其支持的AES算法,Cipher负责完成加密或解密工作
Cipher c = Cipher.getInstance("AES");
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示解密模式
c.init(Cipher.DECRYPT_MODE, desKey);
// 该字节数组负责保存解密的结果,先对str进行Base64解密,将16进制转换为二进制
byte[] cipherByte = c.doFinal(arr);
return cipherByte;
}
public static String secret(MultipartFile multfile) throws Exception {
final File file = File.createTempFile(UUIDUtils.getUUID(), "");
// MultipartFile to File
multfile.transferTo(file);
return FileEncryptUtil.secret(file);
}
//加密
//返回文件名
public static String secret(File file) throws Exception {
String fileName = UUIDUtils.getUUID();
File folder = new File(fileFolder);
if (!folder.exists() && !folder.isDirectory()) {
folder.mkdirs();
System.out.println("创建文件夹");
} else {
System.out.println("文件夹已存在");
}
File destination = new File(filePath + fileName);
// 实现文件下载
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = new FileOutputStream(destination);
int j = bis.read(buffer);
while (j > 0) {
// byte[] bs = Base64ConvertUtil.encode(HexConvertUtil.parseByte2HexStr(buffer)).getBytes();
byte[] bs = FileEncryptUtil.enCrypto(buffer);
os.write(bs, 0, bs.length);
os.flush();
j = bis.read(buffer);
if (j < 1024 && j > 0) {
byte[] tmp = new byte[j];
for (int i = 0; i < j; i++) {
tmp[i] = buffer[i];
}
buffer = tmp;
}
}
os.close();
bis.close();
fis.close();
file.deleteOnExit();
return fileName;
}
public static File decrypt(String filename, String displayName) throws Exception {
int index = displayName.lastIndexOf(".");
String suffix = displayName.substring(index + 1);
String realName = displayName.substring(0, index);
File decrypt = FileEncryptUtil.decrypt(filename, realName, suffix);
return decrypt;
}
//解密
public static File decrypt(String filename, String realName, String suffix) throws Exception {
// 长度1040不可改变,否则无法还原
byte[] buffer = new byte[1040];
File file = new File(filePath + filename);
File destination = new File(filePath + realName + "." + suffix);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = new FileOutputStream(destination);
int j = bis.read(buffer);
while (j > 0) {
byte[] bs = FileEncryptUtil.deCrypto(buffer);
os.write(bs, 0, bs.length);
os.flush();
j = bis.read(buffer);
if (j < 1040 && j > 0) {
byte[] tmp = new byte[j];
for (int i = 0; i < j; i++) {
tmp[i] = buffer[i];
}
buffer = tmp;
}
}
os.close();
bis.close();
fis.close();
return destination;
}
}
解析Excel
首先pom.xml中:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>${easyexcel.version}</version>
</dependency>
解析
public CommonResult CustomBroker(MultipartHttpServletRequest request, @RequestParam("resourceId") String resourceId,
@RequestParam("sourceName") String sourceName, @RequestParam("createBy") String createBy) throws IOException {
MultipartFile file = request.getFile("upload");
InputStream inputStream = file.getInputStream();
ExcelListener<CustomsBroker> listener = new ExcelListener(CustomsBroker.class);
EasyExcel.read(inputStream, listener).head(CustomsBroker.class).sheet().headRowNumber(1).doReadSync();
List<CustomsBroker> list = listener.getAllData();
return CommonResult.success(true);
}
CustomsBroker是实体对象
@TableName(value = "表名")
public class CustomsBroker {
@TableId(value = "uuid", type = IdType.ASSIGN_UUID)//数据库中字段名称
private String uuid;
@TableField(value = "\"name\"")
@ExcelProperty(value = "Name", index = 0)//对应excel中表头名称
private String name;
@TableField(value = "code")
@ExcelProperty(value = "Code", index = 1)
private String code;
}