2023年3月9日16:19:22

master
zhangmeng 2023-03-09 16:19:30 +08:00
parent 1755c45ac3
commit 71aece0cbf
2 changed files with 23 additions and 27 deletions

View File

@ -57,6 +57,8 @@ public class HttpDownLoadController {
private final SimpleObjectProperty<File> file_target = new SimpleObjectProperty<>(); private final SimpleObjectProperty<File> file_target = new SimpleObjectProperty<>();
private static final int DEFAULT_BUFFER_SIZE = 8192;
@FXML @FXML
public void initialize() { public void initialize() {
@ -83,6 +85,8 @@ public class HttpDownLoadController {
return; return;
} }
download_info.setText(null);
MyService myService = new MyService(); MyService myService = new MyService();
myService.progressProperty().addListener((observable, oldValue, newValue) -> { myService.progressProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.doubleValue() >= 1){ if (newValue.doubleValue() >= 1){
@ -110,7 +114,7 @@ public class HttpDownLoadController {
@Override @Override
protected Task<Number> createTask() { protected Task<Number> createTask() {
return new Task<Number>() { return new Task<>() {
@Override @Override
protected Number call() { protected Number call() {
int size = 0; int size = 0;
@ -122,9 +126,8 @@ public class HttpDownLoadController {
download_info.appendText("无法获取文件大小。"); download_info.appendText("无法获取文件大小。");
download_info.appendText(System.lineSeparator()); download_info.appendText(System.lineSeparator());
} else { } else {
download_info.appendText("文件大小为:" + size + " bytes"); download_info.appendText("文件大小为:" + FileUtil.readableFileSize(size));
download_info.appendText(System.lineSeparator()); download_info.appendText(System.lineSeparator());
String fileName; String fileName;
final String path = url.getText(); final String path = url.getText();
// 从路径中获取文件名 // 从路径中获取文件名
@ -134,34 +137,21 @@ public class HttpDownLoadController {
fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8); fileName = URLUtil.encodeQuery(path, CharsetUtil.CHARSET_UTF_8);
} }
String pathText = download_path.getText(); String pathText = download_path.getText();
InputStream fis = conn.getInputStream(); InputStream fis = conn.getInputStream();
int max = size;
FileOutputStream fos = new FileOutputStream(new File(pathText + "/" + fileName)); FileOutputStream fos = new FileOutputStream(new File(pathText + "/" + fileName));
byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
byte[] bytes = new byte[100000]; int i;
int i = 0;
double sum = 0; double sum = 0;
double progress = 0; double progress = 0;
this.updateMessage("开始下载:-------->" + pathText + "/" + fileName); this.updateMessage("开始下载:-------->" + pathText + "/" + fileName);
while ((i = fis.read(bytes, 0, bytes.length)) != -1) { while ((i = fis.read(bytes, 0, DEFAULT_BUFFER_SIZE)) != -1) {
if (this.isCancelled()) { if (this.isCancelled()) {
break; break;
} }
fos.write(bytes, 0, i); fos.write(bytes, 0, i);
sum = sum + i; sum = sum + i;
this.updateProgress(sum, size);
this.updateProgress(sum,max); progress = sum / size;
progress = sum /max;
this.updateMessage("已下载:" + FileUtil.readableFileSize((long) sum)); this.updateMessage("已下载:" + FileUtil.readableFileSize((long) sum));
} }

View File

@ -25,6 +25,8 @@
package com.zhangmeng.tools.utils; package com.zhangmeng.tools.utils;
import javafx.scene.image.Image;
import java.net.URL; import java.net.URL;
/** /**
@ -41,6 +43,10 @@ public class ResourcesUtils {
return ResourcesUtils.class.getResource(path); return ResourcesUtils.class.getResource(path);
} }
public static Image getBg(){
return new Image(ImagePath.path(ImagePath.ImagePathType.BACKGROUND_IMAGE));
}
public enum Menu{ public enum Menu{
Md5("md5加密",0), Md5("md5加密",0),
SpringSecurity("spring加密",1); SpringSecurity("spring加密",1);