用javaswing实现文件拖拽上传,点击脱敏按钮,用另一个类接收swing界面上传文件的内容存入list集合中,对集合进行修改,所有数字+1,返回脱敏后的文件到swing界面中,文件可下载
时间: 2023-06-27 18:03:03 浏览: 116
javaftpclient.zip_Java FTP上传_swing 上传_swing上传下载_swing文件上传
5星 · 资源好评率100%
以下是一个简单的示例代码,实现文件拖拽上传和数字+1的功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileUploadDemo extends JFrame {
private JPanel contentPane;
private JLabel lblFilePath;
private JButton btnUpload;
private JButton btnDesensitize;
private JTextArea taContent;
private File uploadedFile;
public FileUploadDemo() {
setTitle("文件上传示例");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
lblFilePath = new JLabel("拖拽文件到这里上传");
lblFilePath.setPreferredSize(new Dimension(300, 50));
lblFilePath.setBorder(BorderFactory.createLineBorder(Color.BLACK));
lblFilePath.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}
@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
Transferable transferable = support.getTransferable();
try {
List<File> fileList = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
if (fileList.size() > 0) {
uploadedFile = fileList.get(0);
lblFilePath.setText(uploadedFile.getAbsolutePath());
taContent.setText(readFileContent(uploadedFile));
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
northPanel.add(lblFilePath);
btnUpload = new JButton("上传");
btnUpload.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(FileUploadDemo.this);
if (result == JFileChooser.APPROVE_OPTION) {
uploadedFile = fileChooser.getSelectedFile();
lblFilePath.setText(uploadedFile.getAbsolutePath());
taContent.setText(readFileContent(uploadedFile));
}
}
});
northPanel.add(btnUpload);
btnDesensitize = new JButton("脱敏");
btnDesensitize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
List<String> contentList = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new StringReader(taContent.getText()));
String line;
while ((line = reader.readLine()) != null) {
contentList.add(line);
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
for (int i = 0; i < contentList.size(); i++) {
String content = contentList.get(i);
String[] tokens = content.split("\\s+");
StringBuilder sb = new StringBuilder();
for (String token : tokens) {
if (token.matches("\\d+")) {
sb.append(Integer.parseInt(token) + 1);
} else {
sb.append(token);
}
sb.append(" ");
}
contentList.set(i, sb.toString().trim());
}
StringBuilder sb = new StringBuilder();
for (String content : contentList) {
sb.append(content).append("\n");
}
taContent.setText(sb.toString());
}
});
northPanel.add(btnDesensitize);
contentPane.add(northPanel, BorderLayout.NORTH);
taContent = new JTextArea();
JScrollPane scrollPane = new JScrollPane(taContent);
contentPane.add(scrollPane, BorderLayout.CENTER);
setVisible(true);
}
private String readFileContent(File file) {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void main(String[] args) {
new FileUploadDemo();
}
}
```
在这个示例中,我们使用了JFrame和JPanel等swing组件,实现了一个简单的文件上传界面。用户可以通过拖拽或点击“上传”按钮来选择一个文件,文件内容会显示在JTextArea中。
当用户点击“脱敏”按钮时,我们将JTextArea中的内容读取到一个List中,对List中的每一行进行数字+1的操作,最后将修改后的内容显示回JTextArea中。
注意,这里仅仅是对文件内容进行了修改,没有真正地改变上传的文件。如果需要保存修改后的文件,可以添加一个“下载”按钮,将修改后的内容保存到一个新文件中并提供下载链接。
阅读全文