关键词搜索

源码搜索 ×
×

Java免费文件安全加密解密

发布2021-11-17浏览713次

详情内容

官网地址:C#/VB.Net Excel, Word, PowerPoint, PDF Component - Welcome to e-iceblue Company Ltd

 Java集成Excel加解密示例

配置文件

文件枚举

  1. package com.boonya.spring.mybatis.plus.enums;
  2. import lombok.Getter;
  3. @Getter
  4. public enum FileType {
  5. EXCEL(".xls","Excel"),
  6. EXCEL_X(".xls","Excel X"),
  7. JSON(".json","JSON"),
  8. PDF(".pdf","PDF"),
  9. WORD(".doc","WORD"),
  10. WORD_X(".docx","WORD X"),
  11. ;
  12. private String type;
  13. private String desc;
  14. private FileType(String type,String desc){
  15. this.type = type;
  16. this.desc = desc;
  17. }
  18. }

抽象定义

  1. package com.boonya.spring.mybatis.plus.config;
  2. import com.boonya.spring.mybatis.plus.enums.FileType;
  3. import com.spire.xls.Workbook;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Configuration;
  6. import java.io.File;
  7. @Configuration
  8. public abstract class AbstractEncryptDecryptConfig {
  9. /**
  10. * 临时保存路径
  11. */
  12. @Value("${file.encrypt.save.path}")
  13. protected static final String TEMP_URL = "";
  14. /**
  15. * 加密保存路径
  16. */
  17. @Value("${file.encrypt.save.path}")
  18. protected static final String ENCRYPT_URL = "";
  19. /**
  20. * 解密保存路径
  21. */
  22. @Value("${file.decrypt.save.path}")
  23. protected static final String DECRYPT_URL = "";
  24. /**
  25. * 保存操作后的文件
  26. *
  27. * @param wb
  28. * @param targetPath
  29. */
  30. protected void save(Workbook wb, String targetPath) {
  31. File file = new File(targetPath);
  32. if (!file.exists()) {
  33. wb.saveToFile(targetPath);
  34. }
  35. }
  36. public String getEncryptFilePath(String fileName, FileType type) {
  37. return ENCRYPT_URL + File.separator + fileName + type.getType();
  38. }
  39. public String getDecryptFilePath(String fileName, FileType type) {
  40. return DECRYPT_URL + File.separator + fileName + type.getType();
  41. }
  42. /**
  43. * 加密
  44. *
  45. * @param fileName
  46. * @param securityPwd
  47. */
  48. public abstract void encrypt(String fileName, String securityPwd);
  49. /**
  50. * 解密
  51. *
  52. * @param fileName
  53. * @param securityPwd
  54. */
  55. public abstract void decrypt(String fileName, String securityPwd);
  56. }

加密解密

Workbook

  1. package com.boonya.spring.mybatis.plus.config;
  2. import com.boonya.spring.mybatis.plus.enums.FileType;
  3. import com.spire.xls.Workbook;
  4. import org.springframework.stereotype.Component;
  5. import java.io.File;
  6. @Component
  7. public class WorkbookEncryptDecrypt extends AbstractEncryptDecryptConfig {
  8. /**
  9. * 加密Excel
  10. *
  11. * @param fileName
  12. * @param securityPwd
  13. */
  14. public void encrypt(String fileName, String securityPwd) {
  15. //加载Excel文档
  16. Workbook wb = new Workbook();
  17. wb.loadFromFile(TEMP_URL + File.separator + fileName);
  18. //加密工作簿
  19. wb.protect(securityPwd);
  20. //保存文档
  21. String targetPath = getEncryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
  22. save(wb, targetPath);
  23. }
  24. /**
  25. * 解密Excel
  26. *
  27. * @param fileName
  28. * @param securityPwd
  29. */
  30. public void decrypt(String fileName, String securityPwd) {
  31. //加载文档
  32. Workbook wb = new Workbook();
  33. wb.setOpenPassword(securityPwd);
  34. wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
  35. //解除密码保护
  36. wb.unProtect();
  37. //保存文档
  38. String targetPath = getDecryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
  39. save(wb, targetPath);
  40. wb.dispose();
  41. }
  42. }

Worksheet

  1. package com.boonya.spring.mybatis.plus.config;
  2. import com.boonya.spring.mybatis.plus.enums.FileType;
  3. import com.spire.xls.FileFormat;
  4. import com.spire.xls.Workbook;
  5. import com.spire.xls.Worksheet;
  6. import org.springframework.stereotype.Component;
  7. import java.io.File;
  8. @Component
  9. public class WorkSheetEncryptDecrypt extends AbstractEncryptDecryptConfig{
  10. /**
  11. * 加密Excel
  12. * @param fileName
  13. * @param securityPwd
  14. */
  15. public void encrypt(String fileName,String securityPwd){
  16. //加载Excel文档
  17. Workbook wb = new Workbook();
  18. wb.loadFromFile(TEMP_URL + File.separator + fileName);
  19. //获取工作表,加密
  20. Worksheet sheet = wb.getWorksheets().get(0);
  21. sheet.protect(securityPwd);
  22. //保存文档
  23. String targetPath = getEncryptFilePath(fileName ,fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
  24. wb.saveToFile(targetPath, FileFormat.Version2013);
  25. wb.dispose();
  26. }
  27. /**
  28. * 解密Excel
  29. * @param fileName
  30. * @param securityPwd
  31. */
  32. public void decrypt(String fileName,String securityPwd){
  33. //加载文档
  34. Workbook wb = new Workbook();
  35. wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
  36. //获取工作表
  37. Worksheet sheet = wb.getWorksheets().get(0);
  38. //解除工作表的密码保护
  39. sheet.unprotect(securityPwd);
  40. //保存文档
  41. String targetPath = getDecryptFilePath(fileName , fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
  42. wb.saveToFile(targetPath);
  43. wb.dispose();
  44. }
  45. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载