Chinaunix首页 | 论坛 | 博客
  • 博客访问: 164911
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-07-08 19:04:30


文件加密并上传到hdfs中,  分享一下优雅的文件加密方法

hdfs上传每个公司可能不一样, 使用的时候自己调整一下


点击(此处)折叠或打开

  1. import com.meili.erp.common.MsgConstants;
  2. import com.meili.erp.common.exception.ErpException;
  3. import com.meili.erp.util.DateUtil;
  4. import com.meili.erp.util.LogUtil;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.FSDataInputStream;
  7. import org.apache.hadoop.fs.FSDataOutputStream;
  8. import org.apache.hadoop.fs.FileSystem;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.IOUtils;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;

  13. import javax.annotation.PostConstruct;
  14. import javax.crypto.Cipher;
  15. import javax.crypto.CipherInputStream;
  16. import javax.crypto.SecretKey;
  17. import javax.crypto.SecretKeyFactory;
  18. import javax.crypto.spec.DESKeySpec;
  19. import javax.crypto.spec.IvParameterSpec;
  20. import java.io.File;
  21. import java.io.InputStream;
  22. import java.security.SecureRandom;

  23. /**
  24.  * hdfs服务层操作类
  25.  */
  26. @Service
  27. public class HDFSUtil {
  28.     @Value("${hdfs_data_path}")
  29.     private String hdfsDataPath;
  30.     @Value("${hdfs_data_user}")
  31.     private String hdfsUser;

  32.     private Configuration conf;
  33.     private FileSystem hdfs;

  34.     private static final String PASSKEY = "XXXX";
  35.     private static final String DESKEY = "XXXX";


  36.     @PostConstruct
  37.     public void init() {
  38.         conf = new Configuration();
  39.         System.setProperty("HADOOP_USER_NAME", hdfsUser);
  40.         try {
  41.             hdfs = FileSystem.get(conf);
  42.         } catch (Exception e) {
  43.             LogUtil.error("HDFS 初始化失败:" + e.getMessage(), e);
  44.         }
  45.     }

  46.     /**
  47.      * 加密文件,并且上传到hdfs中
  48.      * @param inputStream
  49.      */
  50.     public String upload(InputStream inputStream) throws Exception {
  51.         CipherInputStream cis = null;
  52.         Integer forder = DateUtil.getThisDay();
  53.         String fileName = DateUtil.now().toString();
  54.         SecureRandom sr = new SecureRandom();
  55.         DESKeySpec dks = new DESKeySpec(DESKEY.getBytes("UTF-8"));
  56.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  57.         SecretKey securekey = keyFactory.generateSecret(dks);
  58.         IvParameterSpec iv = new IvParameterSpec(PASSKEY.getBytes("UTF-8"));
  59.         Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  60.         cipher.init(Cipher.ENCRYPT_MODE, securekey, iv, sr);
  61.         cis = new CipherInputStream(inputStream, cipher);

  62.         FSDataOutputStream output = null;
  63.         try {
  64.             String folder = hdfsDataPath + File.separator + forder;
  65.             Path folderPath = new Path(folder);
  66.             if (!hdfs.exists(folderPath)) {
  67.                 hdfs.mkdirs(folderPath);
  68.             }

  69.             Path dst = new Path(folder + File.separator + fileName);
  70.             output = hdfs.create(dst);
  71.             int reads;
  72.             byte buf[] = new byte[8192];
  73.             while ((reads = cis.read(buf)) > 0) {
  74.                 output.write(buf, 0, reads);
  75.             }
  76.         } catch (Exception ex) {
  77.             throw new Exception("附件上传失败|uuid:" + fileName, ex);
  78.         } finally {
  79.             IOUtils.closeStream(inputStream);
  80.             IOUtils.closeStream(cis);
  81.             IOUtils.closeStream(output);
  82.         }
  83.         return forder + File.separator + fileName;
  84.     }

  85.     /**
  86.      * 从hdfs中拿到文件并且解密
  87.      * @param path
  88.      * @return
  89.      * @throws Exception
  90.      */
  91.     public InputStream download(String path) throws Exception {
  92.         SecureRandom sr = new SecureRandom();
  93.         DESKeySpec dks = new DESKeySpec(DESKEY.getBytes("UTF-8"));
  94.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  95.         SecretKey securekey = keyFactory.generateSecret(dks);
  96.         IvParameterSpec iv = new IvParameterSpec(PASSKEY.getBytes("UTF-8"));
  97.         Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  98.         cipher.init(Cipher.DECRYPT_MODE, securekey, iv, sr);

  99.         FSDataInputStream in = null;
  100.         try {
  101.             Path f = new Path(hdfsDataPath + File.separator + path);
  102.             boolean isExists = hdfs.exists(f);
  103.             if (isExists) {
  104.                 in = hdfs.open(f);
  105.                 return new CipherInputStream(in.getWrappedStream(), cipher);
  106.             }
  107.             throw new ErpException(MsgConstants.CONTRACT_DATE_END_IS_EMPTY);
  108.         } catch (ErpException ex) {
  109.             throw ex;
  110.         } catch (Exception ex) {
  111.             throw new Exception("下载附件异常:" + path);
  112.         }

  113.     }

  114. }

阅读(1585) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~