diff --git a/imges/img.png b/imges/img.png new file mode 100644 index 0000000..55acd31 Binary files /dev/null and b/imges/img.png differ diff --git a/imges/img_1.png b/imges/img_1.png new file mode 100644 index 0000000..fc17a35 Binary files /dev/null and b/imges/img_1.png differ diff --git a/imges/img_2.png b/imges/img_2.png new file mode 100644 index 0000000..c5ed240 Binary files /dev/null and b/imges/img_2.png differ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..419881b --- /dev/null +++ b/readme.md @@ -0,0 +1,14 @@ +> 一个简单的minio的客户端 + +![](imges/img.png) + +### 1.功能 + +- 创建bucket +- 上传文件 + +![](imges/img_1.png) + +- url预览 + +![](imges/img_2.png) diff --git a/src/main/java/com/zhangmeng/minio/controller/MinioController.java b/src/main/java/com/zhangmeng/minio/controller/MinioController.java index 5221172..4392d3b 100644 --- a/src/main/java/com/zhangmeng/minio/controller/MinioController.java +++ b/src/main/java/com/zhangmeng/minio/controller/MinioController.java @@ -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 tableview; @FXML - public TableColumn file_name; + public TableColumn file_name; @FXML - public TableColumn file_size; + public TableColumn file_size; @FXML - public TableColumn file_url; + public TableColumn file_url; @FXML - public TableColumn file_bucket; + public TableColumn file_bucket; private File upload_file; @FXML public void initialize() { tableview.setItems(list); - file_name.setCellValueFactory(new PropertyValueFactory("fileName")); - file_size.setCellValueFactory(new PropertyValueFactory("size")); - file_url.setCellValueFactory(new PropertyValueFactory("url")); - file_bucket.setCellValueFactory(new PropertyValueFactory("bucketName")); + file_name.setCellValueFactory(new PropertyValueFactory("fileName")); + file_size.setCellValueFactory(new PropertyValueFactory("size")); + file_url.setCellValueFactory(new PropertyValueFactory("url")); + file_bucket.setCellValueFactory(new PropertyValueFactory("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() { @Override public void changed(ObservableValue 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 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 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 bucketFiles = MinioUtils.getFileList(bucketName); - if (bucketFiles.size()>0){ - list.clear(); + if (bucketFiles.size() > 0) { + list.addAll(bucketFiles); } }