在Java中使用FileChooser选择后加密文件
对于我的新项目,我尝试在gui中选择文档后对其进行编码。我的程序包含两部分,一部分用于选择文件,另一部分用于编码文件。因此,在选择一个文件后,应该调用加密方法,但它实际上不起作用,因为路径似乎没有被采用在Java中使用FileChooser选择后加密文件,java,Java,对于我的新项目,我尝试在gui中选择文档后对其进行编码。我的程序包含两部分,一部分用于选择文件,另一部分用于编码文件。因此,在选择一个文件后,应该调用加密方法,但它实际上不起作用,因为路径似乎没有被采用 public class frame1 { String filepath; public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, Inv
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);
}
}
}
}
可以对程序进行的一个简单更改是将选择器返回的文件传递给
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);
}
}
但是它没有一个绝对路径,用户选择一个文件,或者你的意思是什么?你的程序有很多问题。在哪里将按钮添加到框架?在哪里设置框架可见?您将
文件路径
作为类frame1
中的字段,但您从未实例化该类的对象。@ck1我的错,我更正了它。但是我会在主方法中运行该方法,或者将其粘贴到主方法中。请正确设置代码格式,这很难阅读。你不能在main
中使用encrypt
方法,或者它应该在actionlistener
中吗?它应该在那里,所以当选择文件时,它会被直接编码,但正如你所说,我不能在那里使用它,我不知道如何避免它。我正在尝试运行代码,这似乎很有用,但是由于某些原因javaframe没有出现,你能帮我解决这个问题吗?