Java对CSV文件加密后导入导出功能小结

xiaoxiao2021-02-28  80

技术总结

本次做CSV文件的加密后的导入导出功能,遇到的问题。在这里做个总结,以备不时之需。

1.      加密

加密采用的是DES加密,整个加密类贴出来。

package com.lddsm.util;

 

import java.io.FileInputStream; 

import java.io.FileOutputStream; 

import java.io.InputStream; 

import java.io.OutputStream; 

import java.security.Key; 

import java.security.SecureRandom; 

 

import javax.crypto.Cipher

import javax.crypto.CipherInputStream; 

import javax.crypto.CipherOutputStream; 

import javax.crypto.KeyGenerator;

 

public classEncryptUtil {

   Keykey;  

   public EncryptUtil(Stringstr) {  

      getKey(str);//生成密匙  

   }  

   /** 

    * 根据参数生成KEY 

    */  

   public void getKey(String strKey) {  

      try {  

         KeyGenerator_generator= KeyGenerator.getInstance("DES");  

         _generator.init(new SecureRandom(strKey.getBytes()));  

         this.key =_generator.generateKey();  

         _generator = null;  

      }catch(Exceptione) {  

         throw new RuntimeException("Error initializing SqlMap class. Cause: " +e);  

      }  

   }  

 

   /** 

    * 文件file进行加密并保存目标文件destFile 

    * 

    * @param file  要加密的文件c:/test/srcFile.txt 

    * @param destFile加密后存放的文件名c:/加密后文件.txt 

    */  

   public void encrypt(String file, StringdestFile) throws Exception {  

      Cipher cipher = Cipher.getInstance("DES");  

      // cipher.init(Cipher.ENCRYPT_MODE, getKey());  

      cipher.init(Cipher.ENCRYPT_MODE,this.key);  

      InputStreamis = new FileInputStream(file);  

      OutputStreamout= newFileOutputStream(destFile);  

      CipherInputStreamcis= newCipherInputStream(is,cipher);  

      byte[] buffer = new byte[1024];  

      int r;  

      while ((r =cis.read(buffer)) > 0) {  

         out.write(buffer, 0,r);  

      }  

      cis.close();  

      is.close();  

      out.close();  

   }  

   /** 

    * 文件采用DES算法解密文件 

    * 

    * @param file已加密的文件c:/加密后文件.txt 

    *        * @param destFile 

    *        解密后存放的文件名c:/ test/解密后文件.txt 

    */  

   public void decrypt(String file, Stringdest) throws Exception {  

      Cipher cipher = Cipher.getInstance("DES");  

      cipher.init(Cipher.DECRYPT_MODE,this.key);  

      InputStreamis = new FileInputStream(file);  

      OutputStreamout= newFileOutputStream(dest);  

      CipherOutputStreamcos= newCipherOutputStream(out,cipher);  

      byte[] buffer = new byte[1024];  

      int r;  

      while ((r =is.read(buffer)) >= 0) {  

         System.out.println(); 

         cos.write(buffer, 0,r);  

      }  

      cos.close();  

      out.close();  

      is.close();  

   }  

   /**

    * 创建普通文件

    * @param is输入流

    * @param destFile创建后的路径

    * @throws Exception

    */

   public static void createFile(InputStreamis, String destFile)throws Exception {  

      OutputStreamout= newFileOutputStream(destFile);  

      byte[] buffer = new byte[1024];  

      int r;  

      while ((r =is.read(buffer)) > 0) {  

         out.write(buffer, 0,r);  

      }  

      is.close();  

      out.close();  

   } 

   public static void main(String[] args) throws Exception {

      InputStreaminputStream= newFileInputStream("c:/aaa.txt");

      createFile(inputStream,"c:/bbb.txt");

//    EncryptUtiltd = new EncryptUtil("aaa");  

//    td.encrypt("c:/srctest.txt","c:/srctest解密.txt");//加密  

//    td.decrypt("c:/srctest解密.txt", "c:/r1.txt"); //解密  

 

   }

}

2

2.      导入导出CSV

3. public staticList<String> importCsv(MultipartFile file){

4.          List<String> dataList=newArrayList<String>();

5.          

6.          BufferedReader br=null;

7.          try {

8.              //br = new BufferedReader(new FileReader(file));

9.              br=new BufferedReader(new InputStreamReader(file.getInputStream(),"GBK"));

10.               String line ="";

11.               while ((line =br.readLine()) != null) {

12.                   dataList.add(line);

13.               }

14.           }catch (Exceptione) {

15.           }finally{

16.               if(br!=null){

17.                   try {

18.                       br.close();

19.                       br=null;

20.                   } catch (IOExceptione) {

21.                       e.printStackTrace();

22.                   }

23.               }

24.           }

25.    

26.           returndataList;

27.       }

28.       /**

29.     * 导出

30.     *

31.     * @param filecsv文件(路径+文件名)csv文件不存在会自动创建

32.     * @param dataList数据

33.     * @return

34.     */

35.    public static boolean exportCsv(File file, List<String> dataList){

36.        booleanisSucess=false;

37.        

38.        FileOutputStream out=null;

39.        OutputStreamWriter osw=null;

40.        BufferedWriter bw=null;

41.        try {

42.            out = new FileOutputStream(file);

43.            osw = new OutputStreamWriter(out);

44.            bw =new BufferedWriter(osw);

45.            if(dataList!=null&& !dataList.isEmpty()){

46.                for(Stringdata : dataList){

47.                    bw.append(data).append("\r");

48.                }

49.            }

50.            isSucess=true;

51.        } catch (Exceptione) {

52.            isSucess=false;

53.        }finally{

54.            if(bw!=null){

55.                try {

56.                    bw.close();

57.                    bw=null;

58.                } catch (IOExceptione) {

59.                    e.printStackTrace();

60.                }

61.            }

62.            if(osw!=null){

63.                try {

64.                    osw.close();

65.                    osw=null;

66.                } catch (IOExceptione) {

67.                    e.printStackTrace();

68.                }

69.            }

70.            if(out!=null){

71.                try {

72.                    out.close();

73.                    out=null;

74.                } catch (IOExceptione) {

75.                    e.printStackTrace();

76.                }

77.            }

78.        }

79.        

80.        returnisSucess;

81.    }

82.    

83.    /**

84.     * 导入

85.     *

86.     * @param filecsv文件(路径+文件)

87.     * @return

88.     */

89.    public static List<String> importCsv(File file){

90.        List<String> dataList=newArrayList<String>();

91.        

92.        BufferedReader br=null;

93.        try {

94.            //br = new BufferedReader(new FileReader(file));

95.            br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));

96.            String line = "";

97.            while ((line =br.readLine()) != null) {

98.                dataList.add(line);

99.            }

100.           }catch (Exceptione) {

101.           }finally{

102.               if(br!=null){

103.                   try {

104.                       br.close();

105.                       br=null;

106.                   } catch (IOExceptione) {

107.                       e.printStackTrace();

108.                   }

109.               }

110.           }

111.    

112.           returndataList;

113.      }

 

备注:以上代码直接可以用。下面说说项目开发遇到的问题。

问题1.jsp页面中给${aaa}赋值。

原来我是想把待上传的文件的路径(即input标签的value值) 赋值给${tmpPath},但是${aaa}只能通过<c:set var=”tmpPath” ><%=Config.value%></c:set>来赋值。如下图

其他方式不行,不能通过JS的方式,失败。后面的解决方法如下:

问题2.页面给后台传值问题。

该问题用到了URL传参,用jQuery的$().attr(“action”,  ”path”);修改action,再传送需要的值。如上图。

 

转载请注明原文地址: https://www.6miu.com/read-32658.html

最新回复(0)