2017-07-16 51 views
2

對於我的新項目,我嘗試在gui中選擇文檔後對其進行編碼。我的程序包含兩個部分,一個選擇文件,另一個對其進行編碼。所以在選擇一個文件之後,應該調用加密方法,但它並不真正起作用,因爲路徑似乎沒有被採用。使用Java中的FileChooser進行選擇後加密文件

public class frame1 { 

    String filepath; 

    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JDialog.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("Test"); 
     frame.getContentPane().setLayout(new FlowLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JButton button = new JButton("Encrypt"); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
      JFileChooser fileChooser = new JFileChooser(); 
      FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES","txt", "text"); 
      fileChooser.setFileFilter(filter); 
      int returnValue = fileChooser.showOpenDialog(null); 
      if (returnValue == JFileChooser.APPROVE_OPTION) { 
       File selectedFile = fileChooser.getSelectedFile(); 
        System.out.println(selectedFile.getAbsolutePath()); 
        String filepath = selectedFile.getAbsolutePath(); 

      } 
     frame.getContentPane().add(button); 
     frame.pack(); 
     frame.setVisible(true); 

     public void encrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { 
     KeyGenerator keygenerator = KeyGenerator.getInstance("AES"); 
     SecretKey myDesKey = keygenerator.generateKey(); 
     Cipher desalgCipher; 
     desalgCipher = Cipher.getInstance("AES"); 
     desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey); 

     Path path = Paths.get(filepath); 
     try(InputStream is = Files.newInputStream(path);  
     CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(cipherIS));){ 
      String line; 
      while((line = reader.readLine()) != null){ 
       System.out.println(line);    
      } 
     } 
     }   
} 
+0

但它沒有絕對路徑,用戶選擇一個文件,或者你是什麼意思? –

+0

您的程序存在許多問題。你在哪裏添加按鈕到框架?你在哪裏設置框架可見?你有'filepath'作爲類'frame1'中的一個字段,但你永遠不會實例化該類的一個對象。 – ck1

+0

@ ck1我的不好,我糾正了它。但是我會在主要方法內部運行該方法或將其粘貼到那裏 –

回答

2

一個簡單的改變就可以使你的程序是從選擇器返回的文件傳遞給你的encrypt()方法。例如:

import javax.crypto.*; 
import javax.swing.*; 
import javax.swing.filechooser.FileNameExtensionFilter; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 

public class App { 
    public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JDialog.setDefaultLookAndFeelDecorated(true); 

     JFrame frame = new JFrame("Test"); 
     frame.getContentPane().setLayout(new FlowLayout()); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     JButton button = new JButton("Encrypt"); 
     button.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       JFileChooser fileChooser = new JFileChooser(); 
       FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text"); 
       fileChooser.setFileFilter(filter); 
       int returnValue = fileChooser.showOpenDialog(null); 
       if (returnValue == JFileChooser.APPROVE_OPTION) { 
        File selectedFile = fileChooser.getSelectedFile(); 
        System.out.println(selectedFile.getAbsolutePath()); 
        String filepath = selectedFile.getAbsolutePath(); 
        try { 
         encrypt(filepath); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 

      public void encrypt(String path) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException { 
       KeyGenerator keygenerator = KeyGenerator.getInstance("AES"); 
       SecretKey myDesKey = keygenerator.generateKey(); 
       Cipher desalgCipher; 
       desalgCipher = Cipher.getInstance("AES"); 
       desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey); 

       try (InputStream is = Files.newInputStream(Paths.get(path)); 
        CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(cipherIS));) { 
        String line; 
        while ((line = reader.readLine()) != null) { 
         System.out.println(line); 
        } 
       } 
      } 
     }); 

     frame.getContentPane().add(button); 
     frame.setSize(300, 400); 
     frame.setVisible(true); 
    } 
} 
+0

我試圖運行的代碼似乎有幫助,但由於某種原因,javaframe不顯示,你能幫助我嗎? –

相關問題