QiniuUtils.java
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.code4everything.qiniu.util;
import cn.hutool.core.lang.Validator;
import cn.hutool.http.HttpUtil;
import com.zhazhapan.util.FileExecutor;
import com.zhazhapan.util.Formatter;
import com.zhazhapan.util.ThreadPool;
import com.zhazhapan.util.Utils;
import com.zhazhapan.util.dialog.Alerts;
import org.apache.log4j.Logger;
import org.code4everything.qiniu.QiniuApplication;
import org.code4everything.qiniu.constant.QiniuValueConsts;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
/**
* @author pantao
* @since 2018/4/14
*/
public class QiniuUtils {
private static final Logger LOGGER = Logger.getLogger(QiniuUtils.class);
private static final String HTTP = "http";
private QiniuUtils() {}
/**
* 下载文件
*
* @param url 文件链接
*/
public static void download(String url) {
// 验证存储路径
if (Validator.isEmpty(QiniuApplication.getConfigBean().getStoragePath())) {
// 显示存储路径输入框
String storagePath = DialogUtils.showInputDialog(null, QiniuValueConsts.CONFIG_DOWNLOAD_PATH,
Utils.getCurrentWorkDir());
if (Validator.isEmpty(storagePath)) {
return;
}
QiniuApplication.getConfigBean().setStoragePath(storagePath);
ConfigUtils.writeConfig();
}
final String dest = QiniuApplication.getConfigBean().getStoragePath();
// 下载文件
ThreadPool.executor.execute(() -> HttpUtil.downloadFile(url, dest));
}
/**
* 检查是否连接网络
*/
public static boolean checkNet() {
try {
URL url = new URL("https://www.qiniu.com/");
InputStream in = url.openStream();
in.close();
return true;
} catch (IOException e) {
LOGGER.error("there is no connection to the network");
return false;
}
}
public static void saveLogFile(String file, String content) {
try {
FileExecutor.saveLogFile(file, content);
} catch (IOException e) {
Alerts.showError(QiniuValueConsts.MAIN_TITLE, e.getMessage());
}
}
public static void saveFile(File file, String content) {
try {
FileExecutor.saveFile(file, content);
} catch (IOException e) {
Alerts.showError(QiniuValueConsts.MAIN_TITLE, e.getMessage());
}
}
public static void openLink(String url) {
try {
Utils.openLink(url);
} catch (Exception e) {
Alerts.showError(QiniuValueConsts.MAIN_TITLE, e.getMessage());
}
}
public static String getFileName(String string) {
try {
return Formatter.getFileName(string);
} catch (UnsupportedEncodingException e) {
LOGGER.error("get file name of url failed, message -> " + e.getMessage());
return "";
}
}
/**
* 拼接链接
*/
public static String buildUrl(String fileName, String domain) {
if (!domain.startsWith(HTTP)) {
domain = HTTP + "://" + domain;
}
fileName = fileName.replaceAll(" ", "qn_code_per_20").replaceAll("/", "qn_code_per_2F");
try {
fileName = URLEncoder.encode(fileName, "utf-8").replaceAll("qn_code_per_2F", "/");
return String.format("%s/%s", domain, fileName.replaceAll("qn_code_per_20", "%20"));
} catch (UnsupportedEncodingException e) {
LOGGER.error("encode url failed, message -> " + e.getMessage());
return "";
}
}
}