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

Commit 6efee01

Browse files
committed
添加文件统计
1 parent b746004 commit 6efee01

File tree

10 files changed

+145
-15
lines changed

10 files changed

+145
-15
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,17 @@
3636

3737
其中 `config.json` 为配置文件
3838

39+
- 文件上传
40+
41+
除了可上传本地文件外,还可抓取网络文件到空间中,如:
42+
43+
![上传网络文件](http://img.blog.csdn.net/20171017152757227?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
44+
45+
然后点击开始上传即可
46+
3947
**2. 资源管理界面:**
4048

41-
![资源管理界面](http://img.blog.csdn.net/20171017111653241?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
49+
![资源管理界面](http://img.blog.csdn.net/20171017153112198?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjY5NTQ3NzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
4250

4351
- 刷新列表
4452

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

+4
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,14 @@ public void listFileOfBucket() {
275275
Values.BUCKET_LIST_LIMIT_SIZE, "");
276276
ArrayList<FileInfo> files = new ArrayList<FileInfo>();
277277
logger.info("get file list of bucket: " + bucket);
278+
QiniuApplication.totalLength = 0;
279+
QiniuApplication.totalSize = 0;
278280
// 处理获取的file list结果
279281
while (iterator.hasNext()) {
280282
com.qiniu.storage.model.FileInfo[] items = iterator.next();
281283
for (com.qiniu.storage.model.FileInfo item : items) {
284+
QiniuApplication.totalLength++;
285+
QiniuApplication.totalSize += item.fsize;
282286
// 将七牛的时间单位(100纳秒)转换成毫秒,然后转换成时间
283287
String time = Formatter.timeStampToString(item.putTime / 10000);
284288
String size = Formatter.formatSize(item.fsize);

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

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public class QiniuApplication extends Application {
6767

6868
public static CdnManager cdnManager = null;
6969

70+
/**
71+
* 空间总文件数
72+
*/
73+
public static int totalLength = 0;
74+
75+
/**
76+
* 空间使用总大小
77+
*/
78+
public static long totalSize = 0;
79+
7080
/**
7181
* 主程序入口
7282
*/

src/main/java/com/zhazhapan/qiniu/config/ConfigLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static void writeConfig(String configJson) {
203203
ThreadPool.executor.submit(() -> {
204204
try {
205205
BufferedWriter out = new BufferedWriter(new FileWriter(configPath, false));
206-
out.write(Formatter.jsonFormat(configJson));
206+
out.write(Formatter.formatJson(configJson));
207207
out.close();
208208
logger.info("rewrite configuration success");
209209
} catch (IOException e) {

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javafx.scene.control.ButtonType;
4141
import javafx.scene.control.ComboBox;
4242
import javafx.scene.control.Hyperlink;
43+
import javafx.scene.control.Label;
4344
import javafx.scene.control.SelectionMode;
4445
import javafx.scene.control.TableColumn;
4546
import javafx.scene.control.TableView;
@@ -114,7 +115,13 @@ public class MainWindowController {
114115
private Hyperlink toIntro;
115116

116117
@FXML
117-
Hyperlink toIntro1;
118+
private Hyperlink toIntro1;
119+
120+
@FXML
121+
private Label totalSizeLabel;
122+
123+
@FXML
124+
private Label totalLengthLabel;
118125

119126
private static MainWindowController mainWindowController = null;
120127

@@ -371,20 +378,33 @@ public void searchFile(KeyEvent event) {
371378
ArrayList<FileInfo> files = new ArrayList<FileInfo>();
372379
String search = Checker.checkNull(searchTextField.getText());
373380
logger.info("search file: " + search);
381+
QiniuApplication.totalLength = 0;
382+
QiniuApplication.totalSize = 0;
374383
try {
375384
// 正则匹配查询
376385
Pattern pattern = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
377386
for (FileInfo file : QiniuApplication.data) {
378387
if (pattern.matcher(file.getName()).find()) {
379388
files.add(file);
389+
QiniuApplication.totalLength++;
390+
QiniuApplication.totalSize += Formatter.sizeToLong(file.getSize());
380391
}
381392
}
382393
} catch (Exception e) {
383394
logger.warn("pattern '" + search + "' compile error, message: " + e.getMessage());
384395
}
396+
setBucketCount();
385397
resTable.setItems(FXCollections.observableArrayList(files));
386398
}
387399

400+
/**
401+
* 统计空间文件的数量以及大小
402+
*/
403+
public void setBucketCount() {
404+
totalLengthLabel.setText(Formatter.customFormatDecimal(QiniuApplication.totalLength, ",###") + " 个文件");
405+
totalSizeLabel.setText(Formatter.formatSize(QiniuApplication.totalSize));
406+
}
407+
388408
/**
389409
* 刷新资源列表
390410
*/
@@ -403,8 +423,12 @@ public void refreshResTable() {
403423
public void setResTableData() {
404424
ThreadPool.executor.submit(() -> {
405425
new QiManager().listFileOfBucket();
406-
Platform.runLater(() -> resTable.setItems(QiniuApplication.data));
426+
Platform.runLater(() -> {
427+
resTable.setItems(QiniuApplication.data);
428+
setBucketCount();
429+
});
407430
});
431+
408432
}
409433

410434
/**

src/main/java/com/zhazhapan/qiniu/util/Checker.java

+6
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ public class Checker {
2020

2121
public static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9]+$");
2222

23+
public static final Pattern DECIMAL_PATTERN = Pattern.compile("^[0-9]+(\\.[0-9]+)?$");
24+
2325
public static boolean isDate(String date) {
2426
return isNull(date) ? false : DATE_PATTERN.matcher(date).matches();
2527
}
2628

29+
public static boolean isDecimal(String string) {
30+
return isNull(string) ? false : DECIMAL_PATTERN.matcher(string).matches();
31+
}
32+
2733
public static boolean isNumber(String string) {
2834
return isNull(string) ? false : NUMBER_PATTERN.matcher(string).matches();
2935
}

src/main/java/com/zhazhapan/qiniu/util/Formatter.java

+54-9
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static int stringToInt(String string) {
3232
if (Checker.isNumber(string)) {
3333
return Integer.parseInt(string);
3434
}
35-
return 0;
35+
return -1;
3636
}
3737

3838
public static final Pattern FILE_NAME_PATTERN = Pattern.compile("([^/\\\\:*\"<>|?]+\\.)*[^/\\\\:*\"<>|?]+(\\?.*)?$",
@@ -42,29 +42,74 @@ public static String formatSize(long size) {
4242
if (size < Values.KB) {
4343
return size + " B";
4444
} else if (size < Values.MB) {
45-
return decimalFormat((double) size / Values.KB) + " KB";
45+
return formatDecimal((double) size / Values.KB) + " KB";
4646
} else if (size < Values.GB) {
47-
return decimalFormat((double) size / Values.MB) + " MB";
47+
return formatDecimal((double) size / Values.MB) + " MB";
4848
} else if (size < Values.TB) {
49-
return decimalFormat((double) size / Values.GB) + " GB";
49+
return formatDecimal((double) size / Values.GB) + " GB";
5050
} else {
51-
return decimalFormat((double) size / Values.TB) + " TB";
51+
return formatDecimal((double) size / Values.TB) + " TB";
5252
}
5353
}
5454

55-
public static String decimalFormat(double number) {
56-
return decimalFormat(number, "#0.00");
55+
/**
56+
* 将格式化后的大小转换成long型
57+
*
58+
* @param size
59+
* 格式:34.12 MB
60+
* @return long
61+
*/
62+
public static long sizeToLong(String size) {
63+
if (Checker.isNotEmpty(size)) {
64+
String num = size.split(" ")[0];
65+
double result = 0;
66+
if (size.contains("TB")) {
67+
result = stringToDouble(num) * Values.TB;
68+
} else if (size.contains("GB")) {
69+
result = stringToDouble(num) * Values.GB;
70+
} else if (size.contains("MB")) {
71+
result = stringToDouble(num) * Values.MB;
72+
} else if (size.contains("KB")) {
73+
result = stringToDouble(num) * Values.KB;
74+
} else {
75+
result = stringToDouble(num);
76+
}
77+
return (long) result;
78+
}
79+
return -1;
80+
}
81+
82+
public static double stringToDouble(String s) {
83+
if (Checker.isDecimal(s)) {
84+
return Double.parseDouble(s);
85+
}
86+
return -1;
87+
}
88+
89+
public static long stringToLong(String s) {
90+
if (Checker.isNumber(s)) {
91+
return Long.parseLong(s);
92+
}
93+
return -1;
94+
}
95+
96+
public static String customFormatDecimal(double number, String format) {
97+
return formatDecimal(number, format);
98+
}
99+
100+
public static String formatDecimal(double number) {
101+
return formatDecimal(number, "#0.00");
57102
}
58103

59-
public static String decimalFormat(double number, String format) {
104+
public static String formatDecimal(double number, String format) {
60105
return new DecimalFormat(format).format(number);
61106
}
62107

63108
public static String timeStampToString(long time) {
64109
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
65110
}
66111

67-
public static String jsonFormat(String string) {
112+
public static String formatJson(String string) {
68113
String json;
69114
try {
70115
Gson gson = new GsonBuilder().setPrettyPrinting().create();

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

+10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
<Insets left="10.0" />
126126
</HBox.margin>
127127
</TextField>
128+
<Label fx:id="totalLengthLabel">
129+
<HBox.margin>
130+
<Insets left="10.0" />
131+
</HBox.margin>
132+
</Label>
128133
</children>
129134
<padding>
130135
<Insets left="10.0" />
@@ -208,6 +213,11 @@
208213
<Insets top="10.0" />
209214
</VBox.margin>
210215
</Button>
216+
<Label fx:id="totalSizeLabel">
217+
<VBox.margin>
218+
<Insets top="10.0" />
219+
</VBox.margin>
220+
</Label>
211221
</children>
212222
</VBox>
213223
</children>

src/test/java/com/zhazhapan/qiniu/QiniuApplicationTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class QiniuApplicationTest {
1717

1818
@Test
19-
public void contextLoads() {
20-
}
19+
public void testContext() {
2120

21+
}
2222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*
3+
*/
4+
package com.zhazhapan.qiniu;
5+
6+
import org.junit.Test;
7+
8+
import com.zhazhapan.qiniu.util.Formatter;
9+
10+
/**
11+
* @author pantao
12+
*
13+
*/
14+
public class TestFormatter {
15+
16+
@Test
17+
public void testSizeToLong() {
18+
String[] sizes = { "23.12 MB", "12.89 KB", "23 B", "23.77 GB", "89.12 TB" };
19+
for (String size : sizes) {
20+
System.out.println(Formatter.sizeToLong(size));
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)