From 71aece0cbf9cf78a1fbc89bd1aecaf73ca6ed806 Mon Sep 17 00:00:00 2001 From: zhangmeng <1334717033@qq.com> Date: Thu, 9 Mar 2023 16:19:30 +0800 Subject: [PATCH] =?UTF-8?q?2023=E5=B9=B43=E6=9C=889=E6=97=A516:19:22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HttpDownLoadController.java | 44 +++++++------------ .../zhangmeng/tools/utils/ResourcesUtils.java | 6 +++ 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java b/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java index a169fe6..131c3e3 100644 --- a/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java +++ b/src/main/java/com/zhangmeng/tools/controller/HttpDownLoadController.java @@ -57,6 +57,8 @@ public class HttpDownLoadController { private final SimpleObjectProperty file_target = new SimpleObjectProperty<>(); + private static final int DEFAULT_BUFFER_SIZE = 8192; + @FXML public void initialize() { @@ -83,6 +85,8 @@ public class HttpDownLoadController { return; } + download_info.setText(null); + MyService myService = new MyService(); myService.progressProperty().addListener((observable, oldValue, newValue) -> { if (newValue.doubleValue() >= 1){ @@ -110,9 +114,9 @@ public class HttpDownLoadController { @Override protected Task createTask() { - return new Task() { + return new Task<>() { @Override - protected Number call(){ + protected Number call() { int size = 0; try { URL url1 = new URL(url.getText()); @@ -121,10 +125,9 @@ public class HttpDownLoadController { if (size < 0) { download_info.appendText("无法获取文件大小。"); download_info.appendText(System.lineSeparator()); - }else{ - download_info.appendText("文件大小为:" + size + " bytes"); + } else { + download_info.appendText("文件大小为:" + FileUtil.readableFileSize(size)); download_info.appendText(System.lineSeparator()); - String fileName; final String path = url.getText(); // 从路径中获取文件名 @@ -134,34 +137,21 @@ public class HttpDownLoadController { 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; + byte[] bytes = new byte[DEFAULT_BUFFER_SIZE]; + int i; + double sum = 0; + double progress = 0; this.updateMessage("开始下载:-------->" + pathText + "/" + fileName); - while ((i = fis.read(bytes, 0, bytes.length)) != -1) { - - if (this.isCancelled()){ + while ((i = fis.read(bytes, 0, DEFAULT_BUFFER_SIZE)) != -1) { + if (this.isCancelled()) { break; } - - fos.write(bytes,0,i); - + fos.write(bytes, 0, i); sum = sum + i; - - this.updateProgress(sum,max); - - progress = sum /max; - + this.updateProgress(sum, size); + progress = sum / size; this.updateMessage("已下载:" + FileUtil.readableFileSize((long) sum)); } diff --git a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java index 20f1e15..cac3557 100644 --- a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java +++ b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java @@ -25,6 +25,8 @@ package com.zhangmeng.tools.utils; +import javafx.scene.image.Image; + import java.net.URL; /** @@ -41,6 +43,10 @@ public class ResourcesUtils { return ResourcesUtils.class.getResource(path); } + public static Image getBg(){ + return new Image(ImagePath.path(ImagePath.ImagePathType.BACKGROUND_IMAGE)); + } + public enum Menu{ Md5("md5加密",0), SpringSecurity("spring加密",1);