Skip to content
This repository was archived by the owner on Mar 6, 2024. It is now read-only.

Commit b647b90

Browse files
committed
添加(上传下载)进度显示
1 parent 873a541 commit b647b90

File tree

6 files changed

+97
-45
lines changed

6 files changed

+97
-45
lines changed

src/main/java/com/zhazhapan/qiniu/Downloader.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.log4j.Logger;
1414

1515
import com.zhazhapan.qiniu.config.ConfigLoader;
16+
import com.zhazhapan.qiniu.controller.MainWindowController;
1617
import com.zhazhapan.qiniu.modules.constant.Values;
1718
import com.zhazhapan.qiniu.util.Checker;
1819
import com.zhazhapan.qiniu.util.Formatter;
@@ -28,6 +29,8 @@ public class Downloader {
2829

2930
private Logger logger = Logger.getLogger(Downloader.class);
3031

32+
private double progress = 0;
33+
3134
/**
3235
* 下载文件
3336
*/
@@ -46,6 +49,11 @@ public void downloadFromNet(String downloadURL) {
4649
}
4750
ConfigLoader.writeConfig();
4851
}
52+
MainWindowController main = MainWindowController.getInstance();
53+
Platform.runLater(() -> {
54+
main.downloadProgress.setVisible(true);
55+
main.downloadProgress.setProgress(0);
56+
});
4957
ThreadPool.executor.submit(() -> {
5058
checkDownloadPath();
5159
logger.info("start to download url: " + downloadURL);
@@ -59,10 +67,16 @@ public void downloadFromNet(String downloadURL) {
5967
URL url = new URL(downloadURL);
6068
URLConnection conn = url.openConnection();
6169
InputStream inStream = conn.getInputStream();
70+
double size = conn.getContentLength();
71+
progress = 0;
6272
FileOutputStream fs = new FileOutputStream(file);
6373
byte[] buffer = new byte[1024];
6474
while ((byteread = inStream.read(buffer)) != -1) {
6575
fs.write(buffer, 0, byteread);
76+
Platform.runLater(() -> {
77+
progress += 1024;
78+
main.downloadProgress.setProgress(progress / size);
79+
});
6680
}
6781
inStream.close();
6882
fs.close();
@@ -72,6 +86,7 @@ public void downloadFromNet(String downloadURL) {
7286
logger.error(log);
7387
Platform.runLater(() -> Dialogs.showException(Values.DOWNLOAD_FILE_ERROR, e));
7488
}
89+
Platform.runLater(() -> main.downloadProgress.setVisible(false));
7590
});
7691
}
7792

src/main/java/com/zhazhapan/qiniu/QiManager.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,10 @@ public void privateDownload(String fileName, String domain) {
184184
* 私有下载
185185
*/
186186
public void privateDownload(String fileName, String domain, Downloader downloader) {
187-
try {
188-
String encodedFileName = URLEncoder.encode(fileName, "utf-8");
189-
String publicUrl = String.format("%s/%s", domain, encodedFileName);
190-
// 自定义链接过期时间(小时)
191-
long expireInSeconds = 24;
192-
downloader.downloadFromNet(QiniuApplication.auth.privateDownloadUrl(publicUrl, expireInSeconds));
193-
} catch (UnsupportedEncodingException e) {
194-
urlError(e);
195-
}
187+
// 自定义链接过期时间(小时)
188+
long expireInSeconds = 24;
189+
String publicURL = getPublicURL(fileName, domain);
190+
downloader.downloadFromNet(QiniuApplication.auth.privateDownloadUrl(publicURL, expireInSeconds));
196191
}
197192

198193
public void publicDownload(String fileName, String domain) {
@@ -211,8 +206,8 @@ public void publicDownload(String fileName, String domain, Downloader downloader
211206

212207
public String getPublicURL(String fileName, String domain) {
213208
try {
214-
String encodedFileName = URLEncoder.encode(fileName, "utf-8");
215-
return String.format("%s/%s", domain, encodedFileName);
209+
String encodedFileName = URLEncoder.encode(fileName.replaceAll(" ", "qn_code_per_20"), "utf-8");
210+
return String.format("%s/%s", domain, encodedFileName.replaceAll("qn_code_per_20", "%20"));
216211
} catch (UnsupportedEncodingException e) {
217212
urlError(e);
218213
return null;

src/main/java/com/zhazhapan/qiniu/controller/MainWindowController.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javafx.scene.control.DatePicker;
4545
import javafx.scene.control.Hyperlink;
4646
import javafx.scene.control.Label;
47+
import javafx.scene.control.ProgressBar;
4748
import javafx.scene.control.SelectionMode;
4849
import javafx.scene.control.TableColumn;
4950
import javafx.scene.control.TableView;
@@ -140,8 +141,16 @@ public class MainWindowController {
140141
@FXML
141142
private DatePicker endDate;
142143

144+
@FXML
145+
public ProgressBar uploadProgress;
146+
147+
@FXML
148+
public ProgressBar downloadProgress;
149+
143150
private static MainWindowController mainWindowController = null;
144151

152+
private double upProgress = 0;
153+
145154
private String status = "";
146155

147156
public static MainWindowController getInstance() {
@@ -568,7 +577,11 @@ public void uploadFile() {
568577
}
569578
// 新建一个上传文件的线程
570579
ThreadPool.executor.submit(() -> {
571-
Platform.runLater(() -> uploadStatusTextArea.insertText(0, Values.CONFIGING_UPLOAD_ENVIRONMENT));
580+
Platform.runLater(() -> {
581+
uploadProgress.setVisible(true);
582+
uploadProgress.setProgress(0);
583+
uploadStatusTextArea.insertText(0, Values.CONFIGING_UPLOAD_ENVIRONMENT);
584+
});
572585
String bucket = bucketChoiceCombo.getValue();
573586
// 默认不指定key的情况下,以文件内容的hash值作为文件名
574587
String key = Checker.checkNull(filePrefixCombo.getValue());
@@ -577,6 +590,9 @@ public void uploadFile() {
577590
int end = Values.UPLOADING.length() - 2;
578591
Platform.runLater(
579592
() -> uploadStatusTextArea.deleteText(0, Values.CONFIGING_UPLOAD_ENVIRONMENT.length() - 1));
593+
// 总文件数
594+
double total = paths.length;
595+
upProgress = 0;
580596
for (String path : paths) {
581597
if (Checker.isNotEmpty(path)) {
582598
Platform.runLater(() -> uploadStatusTextArea.insertText(0, Values.UPLOADING));
@@ -614,6 +630,8 @@ public void uploadFile() {
614630
Platform.runLater(() -> {
615631
uploadStatusTextArea.deleteText(0, end);
616632
uploadStatusTextArea.insertText(0, status);
633+
// 设置上传的进度
634+
uploadProgress.setProgress(++upProgress / total);
617635
});
618636
}
619637
Platform.runLater(() -> selectedFileTextArea.deleteText(0, path.length() + (paths.length > 1 ? 1 : 0)));
@@ -623,6 +641,8 @@ public void uploadFile() {
623641
uploadStatusTextArea.positionCaret(0);
624642
// 清空待上传的文件列表
625643
selectedFileTextArea.clear();
644+
// 上传完成时,设置上传进度度不可见
645+
uploadProgress.setVisible(false);
626646
});
627647
// 添加文件前缀到配置文件
628648
if (Checker.isNotEmpty(key) && !QiniuApplication.prefix.contains(key)) {

src/main/java/com/zhazhapan/qiniu/modules/constant/Values.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,6 @@ public class Values {
126126
public static final String BUCKET_BANDWIDTH_COUNT = "空间带宽(KB)";
127127

128128
public static final long DATE_SPAN_OF_THIRTY_ONE = (long) 31 * 24 * 60 * 60 * 1000;
129+
130+
public static final String UPLOADING_OR_DOWNLOADING = "有文件正在上传或下载中,是否确认退出?";
129131
}

src/main/java/com/zhazhapan/qiniu/view/MainWindow.fxml

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<?import javafx.scene.control.Hyperlink?>
1212
<?import javafx.scene.control.Label?>
1313
<?import javafx.scene.control.MenuItem?>
14+
<?import javafx.scene.control.ProgressBar?>
1415
<?import javafx.scene.control.Tab?>
1516
<?import javafx.scene.control.TabPane?>
1617
<?import javafx.scene.control.TableColumn?>
@@ -57,7 +58,7 @@
5758
</TextArea>
5859
<HBox alignment="TOP_CENTER" VBox.vgrow="NEVER">
5960
<children>
60-
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#selectFile" prefHeight="27.0" prefWidth="156.0" text="选择文件" HBox.hgrow="ALWAYS">
61+
<Button contentDisplay="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#selectFile" prefHeight="27.0" prefWidth="156.0" text="选择文件" HBox.hgrow="ALWAYS">
6162
<HBox.margin>
6263
<Insets right="10.0" />
6364
</HBox.margin>
@@ -277,7 +278,7 @@
277278
<VBox prefHeight="58.0" prefWidth="600.0" VBox.vgrow="ALWAYS" />
278279
<VBox prefHeight="548.0" prefWidth="643.0">
279280
<children>
280-
<Label text="当前版本: v1.0.4">
281+
<Label text="当前版本: v1.0.5">
281282
<font>
282283
<Font size="20.0" />
283284
</font>
@@ -446,37 +447,43 @@
446447
</TabPane>
447448
</center>
448449
<top>
449-
<HBox BorderPane.alignment="CENTER">
450-
<children>
451-
<ComboBox fx:id="bucketChoiceCombo" minHeight="-Infinity" minWidth="-Infinity" prefHeight="27.0" prefWidth="205.0" promptText="还没有选择存储空间" HBox.hgrow="NEVER">
452-
<HBox.margin>
453-
<Insets left="10.0" />
454-
</HBox.margin>
455-
</ComboBox>
456-
<TextField fx:id="zoneText" editable="false" promptText="存储区域" HBox.hgrow="NEVER">
457-
<HBox.margin>
458-
<Insets left="10.0" />
459-
</HBox.margin>
460-
</TextField>
461-
<Button fx:id="bucketAddableButton" mnemonicParsing="false" onAction="#showBucketAddableDialog" text="添加存储空间" HBox.hgrow="NEVER">
462-
<HBox.margin>
463-
<Insets left="10.0" />
464-
</HBox.margin>
465-
</Button>
466-
<Button fx:id="resetKeyButton" mnemonicParsing="false" onAction="#resetKey" text="重置密钥" HBox.hgrow="NEVER">
467-
<HBox.margin>
468-
<Insets left="10.0" />
469-
</HBox.margin>
470-
</Button>
471-
<Button mnemonicParsing="false" onAction="#openConfigFile" text="打开配置文件" HBox.hgrow="NEVER">
472-
<HBox.margin>
473-
<Insets left="10.0" />
474-
</HBox.margin>
475-
</Button>
476-
</children>
450+
<VBox BorderPane.alignment="CENTER">
477451
<BorderPane.margin>
478-
<Insets bottom="10.0" top="10.0" />
452+
<Insets />
479453
</BorderPane.margin>
480-
</HBox>
454+
<children>
455+
<ProgressBar fx:id="uploadProgress" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="10.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="10.0" prefWidth="500.0" progress="0.0" style="-fx-border-color: transparent;" visible="false" VBox.vgrow="NEVER" />
456+
<HBox VBox.vgrow="NEVER">
457+
<children>
458+
<ComboBox fx:id="bucketChoiceCombo" minHeight="-Infinity" minWidth="-Infinity" prefHeight="27.0" prefWidth="205.0" promptText="还没有选择存储空间" HBox.hgrow="NEVER">
459+
<HBox.margin>
460+
<Insets left="10.0" />
461+
</HBox.margin>
462+
</ComboBox>
463+
<TextField fx:id="zoneText" editable="false" promptText="存储区域" HBox.hgrow="NEVER">
464+
<HBox.margin>
465+
<Insets left="10.0" />
466+
</HBox.margin>
467+
</TextField>
468+
<Button fx:id="bucketAddableButton" mnemonicParsing="false" onAction="#showBucketAddableDialog" text="添加存储空间" HBox.hgrow="NEVER">
469+
<HBox.margin>
470+
<Insets left="10.0" />
471+
</HBox.margin>
472+
</Button>
473+
<Button fx:id="resetKeyButton" mnemonicParsing="false" onAction="#resetKey" text="重置密钥" HBox.hgrow="NEVER">
474+
<HBox.margin>
475+
<Insets left="10.0" />
476+
</HBox.margin>
477+
</Button>
478+
<Button mnemonicParsing="false" onAction="#openConfigFile" text="打开配置文件" HBox.hgrow="NEVER">
479+
<HBox.margin>
480+
<Insets left="10.0" />
481+
</HBox.margin>
482+
</Button>
483+
</children>
484+
</HBox>
485+
<ProgressBar fx:id="downloadProgress" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="10.0" prefHeight="10.0" prefWidth="500.0" progress="0.0" style="-fx-border-color: transparent;" visible="false" VBox.vgrow="NEVER" />
486+
</children>
487+
</VBox>
481488
</top>
482489
</BorderPane>

src/main/java/com/zhazhapan/qiniu/view/MainWindow.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.zhazhapan.qiniu.view;
55

66
import java.util.Date;
7+
import java.util.Optional;
78

89
import org.apache.log4j.Logger;
910

@@ -18,8 +19,10 @@
1819

1920
import javafx.fxml.FXMLLoader;
2021
import javafx.scene.Scene;
22+
import javafx.scene.control.ButtonType;
2123
import javafx.scene.layout.BorderPane;
2224
import javafx.stage.Stage;
25+
import javafx.stage.WindowEvent;
2326

2427
/**
2528
* @author pantao
@@ -55,7 +58,17 @@ public void init() {
5558
Dialogs.showFatalError(Values.INIT_APP_ERROR_HEADER, e);
5659
}
5760
stage.setTitle(Values.MAIN_TITLE);
58-
stage.setOnCloseRequest((e) -> {
61+
stage.setOnCloseRequest((WindowEvent event) -> {
62+
// 判断是否有文件在上传下载
63+
MainWindowController main = MainWindowController.getInstance();
64+
if (main.downloadProgress.isVisible() || main.uploadProgress.isVisible()) {
65+
Optional<ButtonType> result = Dialogs.showConfirmation(Values.UPLOADING_OR_DOWNLOADING);
66+
if (result.get() != ButtonType.OK) {
67+
// 取消退出事件
68+
event.consume();
69+
return;
70+
}
71+
}
5972
ThreadPool.shutdown();
6073
ConfigLoader.checkWorkPath();
6174
FileExecutor executor = new FileExecutor();

0 commit comments

Comments
 (0)