diff --git a/README.md b/README.md index 4417983..7a624ef 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,22 @@ ![](./src/main/resources/static/redame/img_21.png) +### 7.http 工具 + +> 使用hutool工具 + +### 7.1 http 请求工具 + +![](./src/main/resources/static/redame/img_24.png) + +#### 7.2 上传工具 + +![](./src/main/resources/static/redame/img_22.png) + +#### 7.3 下载工具 + +![](./src/main/resources/static/redame/img_23.png) + ## 2. 开源项目总览 | 项目名称 |地址| diff --git a/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java b/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java index 4de7da7..a169fe6 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java @@ -1,7 +1,34 @@ package com.zhangmeng.tools.controller; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.StreamProgress; +import cn.hutool.core.lang.Console; +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.URLUtil; +import cn.hutool.http.HttpUtil; +import com.zhangmeng.tools.utils.AlertUtils; +import javafx.application.Platform; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.concurrent.Service; +import javafx.concurrent.Task; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.ProgressBar; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.stage.DirectoryChooser; +import javafx.stage.Stage; import lombok.extern.slf4j.Slf4j; +import java.io.*; +import java.net.URL; +import java.net.URLConnection; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + /** * @author : 芊芊墨客 * @version : 1.0 @@ -9,4 +36,148 @@ import lombok.extern.slf4j.Slf4j; */ @Slf4j public class HttpDownLoadController { + + @FXML + public ProgressBar progress; + + @FXML + public TextField url; + + @FXML + public Button download; + + @FXML + public TextArea download_info; + + @FXML + private TextField download_path; + + @FXML + private Button choose_file; + + private final SimpleObjectProperty file_target = new SimpleObjectProperty<>(); + + @FXML + public void initialize() { + + choose_file.setOnAction(event -> { + Stage stage = new Stage(); + DirectoryChooser dc = new DirectoryChooser(); + dc.setTitle("文件夹选择器"); + File file = dc.showDialog(stage); + if (file != null){ + download_path.setText(file.getPath()); + file_target.setValue(file); + } + }); + + download.setOnAction(event -> { + + if (url.getText().length() == 0 ){ + AlertUtils.alert_warning("请输入下载文件的地址再试!"); + return; + } + + if (download_path.getText().length() == 0){ + AlertUtils.alert_warning("请输入文件保存地址再试!"); + return; + } + + MyService myService = new MyService(); + myService.progressProperty().addListener((observable, oldValue, newValue) -> { + if (newValue.doubleValue() >= 1){ + myService.cancel(); + log.info("任务取消!......."); + }else { + progress.setProgress(newValue.doubleValue()); + } + }); + + myService.messageProperty().addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, String oldValue, String newValue) { + if(newValue != null){ + download_info.appendText(newValue); + download_info.appendText(System.lineSeparator()); + } + } + }); + myService.start(); + }); + } + + class MyService extends Service{ + + @Override + protected Task createTask() { + return new Task() { + @Override + protected Number call(){ + int size = 0; + try { + URL url1 = new URL(url.getText()); + URLConnection conn = url1.openConnection(); + size = conn.getContentLength(); + if (size < 0) { + download_info.appendText("无法获取文件大小。"); + download_info.appendText(System.lineSeparator()); + }else{ + download_info.appendText("文件大小为:" + size + " bytes"); + download_info.appendText(System.lineSeparator()); + + String fileName; + final String path = url.getText(); + // 从路径中获取文件名 + fileName = StrUtil.subSuf(path, path.lastIndexOf('/') + 1); + if (StrUtil.isBlank(fileName)) { + // 编码后的路径做为文件名 + fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8); + } + String pathText = download_path.getText(); + + InputStream fis = conn.getInputStream(); + int max = size; + + FileOutputStream fos = new FileOutputStream(new File(pathText + "/" + fileName)); + + byte[] bytes = new byte[100000]; + + int i = 0; + + double sum = 0 ; + + double progress = 0; + this.updateMessage("开始下载:-------->" + pathText + "/" + fileName); + while ((i = fis.read(bytes, 0, bytes.length)) != -1) { + + if (this.isCancelled()){ + break; + } + + fos.write(bytes,0,i); + + sum = sum + i; + + this.updateProgress(sum,max); + + progress = sum /max; + + this.updateMessage("已下载:" + FileUtil.readableFileSize((long) sum)); + } + + fis.close(); + fos.close(); + + this.updateMessage("下载结束:-------->" + FileUtil.readableFileSize(size)); + return progress; + } + } catch (IOException e) { + download_info.appendText(e.getMessage()); + download_info.appendText(System.lineSeparator()); + } + return null; + } + }; + } + } } diff --git a/src/main/java/com/zhangmeng/tools/controller/HttpToolsController.java b/src/main/java/com/zhangmeng/tools/controller/HttpToolsController.java index 9d98cd4..1d4a684 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HttpToolsController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HttpToolsController.java @@ -243,14 +243,14 @@ public class HttpToolsController { } if (newValue.getIndex() == 1) { - if (http_request != null){ + if (http_upload != null){ flag = true; } http_upload(flag); } if (newValue.getIndex() == 2) { - if (http_request != null){ + if (http_download != null){ flag = true; } http_download(flag); @@ -349,9 +349,9 @@ public class HttpToolsController { } catch (IOException e) { e.printStackTrace(); } - http_request = root; + http_upload = root; }else { - root = http_request; + root = http_upload; } common_method(); } @@ -366,9 +366,9 @@ public class HttpToolsController { } catch (IOException e) { e.printStackTrace(); } - http_request = root; + http_download = root; }else { - root = http_request; + root = http_download; } common_method(); } diff --git a/src/main/java/com/zhangmeng/tools/controller/HttpUploadController.java b/src/main/java/com/zhangmeng/tools/controller/HttpUploadController.java index 1790118..fde5378 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HttpUploadController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HttpUploadController.java @@ -1,14 +1,23 @@ package com.zhangmeng.tools.controller; +import cn.hutool.core.io.FileUtil; +import cn.hutool.http.HttpUtil; +import com.zhangmeng.tools.utils.AlertUtils; import com.zhangmeng.tools.utils.ImagePath; +import javafx.beans.property.SimpleObjectProperty; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.stage.FileChooser; +import javafx.stage.Stage; import lombok.extern.slf4j.Slf4j; +import java.io.File; +import java.util.HashMap; + /** * @author : 芊芊墨客 * @version : 1.0 @@ -20,21 +29,67 @@ public class HttpUploadController { @FXML public Button upload_button; + @FXML + public Button choose_file_button; + @FXML public TextArea upload_result; @FXML public TextField url; + @FXML + private TextField file_path; + + private final SimpleObjectProperty file_vale = new SimpleObjectProperty<>(); + @FXML public void initialize() { - upload_button.setText(null); + + upload_result.setWrapText(true); + + choose_file_button.setText(null); ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE))); iv.setPreserveRatio(true); iv.setFitWidth(18); - upload_button.setGraphic(iv); + choose_file_button.setGraphic(iv); + choose_file_button.setOnAction(event -> { + Stage stage = new Stage(); + FileChooser fc = new FileChooser(); + fc.setTitle("单选文件"); + fc.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("所有类型", "*.*") + ); + File file = fc.showOpenDialog(stage); + if (file == null) { + return; + } + file_path.setText(file.getAbsolutePath()); + file_vale.setValue(file); + }); + + upload_button.setOnAction(event -> { + + if (url.getText().length() == 0){ + AlertUtils.alert_warning("请输入上传接口再试!"); + return; + } + + HashMap paramMap = new HashMap<>(); + //文件上传只需将参数中的键指定(默认file),值设为文件对象即可,对于使用者来说,文件上传与普通表单提交并无区别 + + if (file_vale.getValue() == null){ + AlertUtils.alert_warning("请选择文件后再试!"); + return; + } + + paramMap.put("file", file_vale.getValue()); + + String result= HttpUtil.post(url.getText(), paramMap); + upload_result.setText(result); + }); } } diff --git a/src/main/resources/fxml/http-download.fxml b/src/main/resources/fxml/http-download.fxml index 1a871e3..af0a14e 100644 --- a/src/main/resources/fxml/http-download.fxml +++ b/src/main/resources/fxml/http-download.fxml @@ -1,11 +1,23 @@ - - - - - - - + + + + + + + + + +