2025年4月21日16:47:17

master
qmstyle 2025-04-21 16:47:23 +08:00
parent 9699a6f811
commit a345943568
5 changed files with 104 additions and 51 deletions

BIN
imges/img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
imges/img_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
imges/img_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

14
readme.md Normal file
View File

@ -0,0 +1,14 @@
> 一个简单的minio的客户端
![](imges/img.png)
### 1.功能
- 创建bucket
- 上传文件
![](imges/img_1.png)
- url预览
![](imges/img_2.png)

View File

@ -21,6 +21,11 @@ import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@ -83,26 +88,26 @@ public class MinioController {
public TableView<BucketFile> tableview;
@FXML
public TableColumn<BucketFile,String> file_name;
public TableColumn<BucketFile, String> file_name;
@FXML
public TableColumn<BucketFile,Number> file_size;
public TableColumn<BucketFile, Number> file_size;
@FXML
public TableColumn<BucketFile,String> file_url;
public TableColumn<BucketFile, String> file_url;
@FXML
public TableColumn<BucketFile,String> file_bucket;
public TableColumn<BucketFile, String> file_bucket;
private File upload_file;
@FXML
public void initialize() {
tableview.setItems(list);
file_name.setCellValueFactory(new PropertyValueFactory<BucketFile,String>("fileName"));
file_size.setCellValueFactory(new PropertyValueFactory<BucketFile,Number>("size"));
file_url.setCellValueFactory(new PropertyValueFactory<BucketFile,String>("url"));
file_bucket.setCellValueFactory(new PropertyValueFactory<BucketFile,String>("bucketName"));
file_name.setCellValueFactory(new PropertyValueFactory<BucketFile, String>("fileName"));
file_size.setCellValueFactory(new PropertyValueFactory<BucketFile, Number>("size"));
file_url.setCellValueFactory(new PropertyValueFactory<BucketFile, String>("url"));
file_bucket.setCellValueFactory(new PropertyValueFactory<BucketFile, String>("bucketName"));
file_name.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size()));
file_size.prefWidthProperty().bind(tableview.widthProperty().divide(tableview.getColumns().size()));
@ -111,10 +116,31 @@ public class MinioController {
// 创建上下文菜单
final ContextMenu contextMenu = new ContextMenu();
MenuItem copyUrl = new MenuItem("copy url");
copyUrl.setOnAction(event -> {
BucketFile selectedItem = tableview.getSelectionModel().getSelectedItem();
// 获取系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 创建一个 StringSelection 对象,该对象封装了要复制的文本
Transferable transferableText = new StringSelection(selectedItem.getUrl());
// 将文本内容设置到剪贴板
clipboard.setContents(transferableText, null);
});
MenuItem menuItem = new MenuItem("删除");
menuItem.setOnAction(event -> {
list.remove(tableview.getSelectionModel().getSelectedItem());
// MinioUtils.delete_file();
});
contextMenu.getItems().add(copyUrl);
contextMenu.getItems().add(menuItem);
// 添加事件监听器来处理上下文菜单的显示
tableview.setOnContextMenuRequested(event -> {
@ -147,7 +173,7 @@ public class MinioController {
fileInputStream = new FileInputStream(ResourceUtils.getPropertiesFile());
byte[] bytes = fileInputStream.readAllBytes();
text_area.setText(new String(bytes, StandardCharsets.UTF_8));
Stage stage = AlertUtils.alert("minio 配置文件",root,600,410,(Stage) MinioUtils.objectMap.get(MinioUtils.primaryStage));
Stage stage = AlertUtils.alert("minio 配置文件", root, 600, 410, (Stage) MinioUtils.objectMap.get(MinioUtils.primaryStage));
save_p.setOnAction(event1 -> {
stage.close();
try {
@ -165,7 +191,7 @@ public class MinioController {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null){
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
@ -178,10 +204,13 @@ public class MinioController {
KeyCombination kc2 = KeyCombination.valueOf("ctrl+r");
reload_config.setAccelerator(kc2);
reload_config.setOnAction(event -> {
load_property();
});
KeyCombination kc3 = KeyCombination.valueOf("ctrl+b");
bucket_choose.setAccelerator(kc3);
MinioUtils.objectMap.put(MinioUtils.minioController,this);
MinioUtils.objectMap.put(MinioUtils.minioController, this);
choose_upload_file.setOnAction(event -> {
//打开文件
@ -190,7 +219,7 @@ public class MinioController {
dc.setTitle("文件选择");
dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("文件类型", "*.*"));
File file = dc.showOpenDialog(stage);
if (file != null){
if (file != null) {
//展示路径
upload_file = file;
upload_path.setText(file.getAbsolutePath());
@ -199,35 +228,37 @@ public class MinioController {
upload.setOnAction(event -> {
if (upload_file == null){
if (upload_file == null) {
AlertUtils.alert_warning("请选择上传的文件后再试!");
return;
}
MinioUtils.upload_file(upload_file);
MinioUtils.upload_file(upload_file);
reload();
});
bucket_btn.setOnAction(event -> {
if (endpoint.getText().length() == 0 ){
if (endpoint.getText().length() == 0) {
AlertUtils.alert_warning("endpoint 不能为空!");
return;
}
if (accessKey.getText().length() == 0 ){
if (accessKey.getText().length() == 0) {
AlertUtils.alert_warning("accessKey 不能为空!");
return;
}
if (secretKey.getText().length() == 0 ){
if (secretKey.getText().length() == 0) {
AlertUtils.alert_warning("secretKey 不能为空!");
return;
}
if (bucket_name.getText().length() == 0 ){
if (bucket_name.getText().length() == 0) {
AlertUtils.alert_warning("bucket_name 不能为空!");
return;
}
MinioUtils.createBucket(bucket_name.getText());
load_bucket_list();
});
load_property();
@ -237,26 +268,27 @@ public class MinioController {
backet_list.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (newValue != null){
MinioUtils.objectMap.put(MinioUtils.current_bucket,newValue);
if (newValue != null) {
MinioUtils.objectMap.put(MinioUtils.current_bucket, newValue);
reload();
}
}
});
backet_list.setItems(backet_item_list);
}
public void save_properties(){
public void save_properties() {
if (endpoint.getText().length() == 0 ){
if (endpoint.getText().length() == 0) {
AlertUtils.alert_warning("endpoint 不能为空!");
return;
}
if (accessKey.getText().length() == 0 ){
if (accessKey.getText().length() == 0) {
AlertUtils.alert_warning("accessKey 不能为空!");
return;
}
if (secretKey.getText().length() == 0 ){
if (secretKey.getText().length() == 0) {
AlertUtils.alert_warning("secretKey 不能为空!");
return;
}
@ -264,7 +296,7 @@ public class MinioController {
// 创建一个Properties对象
Properties properties = new Properties();
File file = ResourceUtils.getPropertiesFile();
if (file != null){
if (file != null) {
InputStream input = null;
OutputStream output = null;
try {
@ -272,10 +304,10 @@ public class MinioController {
output = new FileOutputStream(file);
// load a properties file
properties.load(input);
properties.put("endpoint",this.endpoint.getText());
properties.put("accessKey",this.accessKey.getText());
properties.put("secretKey",this.secretKey.getText());
properties.store(output,"save");
properties.put("endpoint", this.endpoint.getText());
properties.put("accessKey", this.accessKey.getText());
properties.put("secretKey", this.secretKey.getText());
properties.store(output, "save");
} catch (IOException e) {
e.printStackTrace();
} finally {
@ -286,10 +318,10 @@ public class MinioController {
e.printStackTrace();
}
}
if (output != null){
if (output != null) {
try {
output.close();
}catch (IOException e){
} catch (IOException e) {
e.printStackTrace();
}
}
@ -299,10 +331,23 @@ public class MinioController {
}
}
public void load_property(){
public void load_bucket_list() {
//刷新bucket列表
//获取所有储存桶
backet_item_list.clear();
List<Bucket> buckets = MinioUtils.getAllBuckets();
if (buckets.size() > 0) {
for (Bucket bucket : buckets) {
backet_item_list.add(bucket.name());
}
}
this.backet_list.getSelectionModel().select(bucket_name.getText());
}
public void load_property() {
Properties prop = new Properties();
File file = ResourceUtils.getPropertiesFile();
if (file != null){
if (file != null) {
InputStream input = null;
try {
input = new FileInputStream(file);
@ -316,9 +361,9 @@ public class MinioController {
String secretKey = prop.getProperty("secretKey");
this.secretKey.setText(secretKey);
MinioUtils.objectMap.put("endpoint",endpoint);
MinioUtils.objectMap.put("accessKey",accessKey);
MinioUtils.objectMap.put("secretKey",secretKey);
MinioUtils.objectMap.put("endpoint", endpoint);
MinioUtils.objectMap.put("accessKey", accessKey);
MinioUtils.objectMap.put("secretKey", secretKey);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
@ -332,20 +377,13 @@ public class MinioController {
}
}
//获取所有储存桶
List<Bucket> buckets = MinioUtils.getAllBuckets();
if (buckets.size()>0){
for (Bucket bucket : buckets) {
backet_item_list.add(bucket.name());
}
}
this.backet_list.setItems(backet_item_list);
load_bucket_list();
}
@FXML
private void bucket_choose_action(){
private void bucket_choose_action() {
Stage primaryStage = (Stage) this.choose_upload_file.getScene().getWindow();
FXMLLoader fxmlLoader =new FXMLLoader( getClass().getResource("/fxml/bucket_choose.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/bucket_choose.fxml"));
AnchorPane bucket_choose = null;
try {
bucket_choose = fxmlLoader.load();
@ -353,20 +391,21 @@ public class MinioController {
e.printStackTrace();
}
Stage stage = AlertUtils.alert("设置", bucket_choose, primaryStage);
MinioUtils.objectMap.put(MinioUtils.bucket_choose_action_stage,stage);
MinioUtils.objectMap.put(MinioUtils.bucket_choose_action_stage, stage);
}
@FXML
public void reload(){
public void reload() {
Object obj = MinioUtils.objectMap.get(MinioUtils.current_bucket);
if (obj == null){
if (obj == null) {
AlertUtils.alert_warning("请选择bucket再试");
return;
}
list.clear();
String bucketName = obj.toString();
List<BucketFile> bucketFiles = MinioUtils.getFileList(bucketName);
if (bucketFiles.size()>0){
list.clear();
if (bucketFiles.size() > 0) {
list.addAll(bucketFiles);
}
}