处理服务指令资源同步后的下载
This commit is contained in:
parent
6ba043facf
commit
d8c48e0a36
|
@ -3,6 +3,7 @@ package com.nu.utils;
|
||||||
import com.google.common.util.concurrent.RateLimiter;
|
import com.google.common.util.concurrent.RateLimiter;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@ -18,50 +19,56 @@ public class FileDownloader {
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static String downloadFile(String fileUrl, String saveDir, String fileName) throws Exception {
|
public static String downloadFile(String fileUrl, String saveDir, String fileName) throws Exception {
|
||||||
// 标准化URL路径
|
// 标准化 URL
|
||||||
fileUrl = normalizeUrl(fileUrl);
|
fileUrl = normalizeUrl(fileUrl);
|
||||||
|
|
||||||
// 创建保存目录
|
// 准备保存目录
|
||||||
File dir = new File(saveDir);
|
File dir = new File(saveDir);
|
||||||
if (!dir.exists()) {
|
if (!dir.exists() && !dir.mkdirs()) {
|
||||||
dir.mkdirs();
|
throw new IOException("创建目录失败: " + saveDir);
|
||||||
|
}
|
||||||
|
if (!dir.canWrite()) {
|
||||||
|
throw new IOException("目录不可写: " + saveDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建完整保存路径
|
// 构建完整保存路径
|
||||||
String savePath = Paths.get(saveDir, fileName).toString();
|
String savePath = Paths.get(saveDir, fileName).toString();
|
||||||
File outputFile = new File(savePath);
|
File outputFile = new File(savePath);
|
||||||
|
|
||||||
// 如果文件已存在,直接返回路径
|
|
||||||
if (outputFile.exists()) {
|
if (outputFile.exists()) {
|
||||||
return savePath;
|
return savePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建RateLimiter 限制下载速度为100k/s
|
// 先用 HttpURLConnection 检查资源状态
|
||||||
RateLimiter rateLimiter = RateLimiter.create(100*1024);
|
|
||||||
|
|
||||||
// 打开URL连接
|
|
||||||
URL url = new URL(fileUrl);
|
URL url = new URL(fileUrl);
|
||||||
InputStream in = url.openStream();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setConnectTimeout(10_000);
|
||||||
|
conn.setReadTimeout(30_000);
|
||||||
|
conn.setRequestMethod("GET");
|
||||||
|
conn.connect();
|
||||||
|
|
||||||
try (BufferedInputStream bis = new BufferedInputStream(in);
|
// 限速:100 KB/s
|
||||||
|
RateLimiter rateLimiter = RateLimiter.create(100 * 1024);
|
||||||
|
|
||||||
|
// 真正开始下载
|
||||||
|
try (InputStream in = conn.getInputStream();
|
||||||
|
BufferedInputStream bis = new BufferedInputStream(in);
|
||||||
FileOutputStream fos = new FileOutputStream(outputFile);
|
FileOutputStream fos = new FileOutputStream(outputFile);
|
||||||
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
BufferedOutputStream bos = new BufferedOutputStream(fos)) {
|
||||||
|
|
||||||
byte[] buffer = new byte[8192]; // 8KB缓冲区
|
byte[] buffer = new byte[8 * 1024];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
|
|
||||||
while ((bytesRead = bis.read(buffer)) != -1) {
|
while ((bytesRead = bis.read(buffer)) != -1) {
|
||||||
// 获取所需数量的许可
|
|
||||||
rateLimiter.acquire(bytesRead);
|
rateLimiter.acquire(bytesRead);
|
||||||
|
|
||||||
// 写入文件
|
|
||||||
bos.write(buffer, 0, bytesRead);
|
bos.write(buffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
return savePath;
|
return savePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 标准化URL路径(处理开头/结尾的斜杠)
|
* 标准化URL路径(处理开头/结尾的斜杠)
|
||||||
*
|
*
|
||||||
|
|
|
@ -254,7 +254,7 @@ jeecg:
|
||||||
app: http://localhost:8051
|
app: http://localhost:8051
|
||||||
path:
|
path:
|
||||||
#文件上传根目录 设置
|
#文件上传根目录 设置
|
||||||
upload: /opt/ope/upFiles101
|
upload: /opt/biz/upFiles101
|
||||||
#webapp文件路径
|
#webapp文件路径
|
||||||
webapp: /opt/biz101/webapp
|
webapp: /opt/biz101/webapp
|
||||||
shiro:
|
shiro:
|
||||||
|
|
|
@ -254,7 +254,7 @@ jeecg:
|
||||||
app: http://localhost:8051
|
app: http://localhost:8051
|
||||||
path:
|
path:
|
||||||
#文件上传根目录 设置
|
#文件上传根目录 设置
|
||||||
upload: /opt/ope/upFiles102
|
upload: /opt/biz/upFiles102
|
||||||
#webapp文件路径
|
#webapp文件路径
|
||||||
webapp: /opt/biz102/webapp
|
webapp: /opt/biz102/webapp
|
||||||
shiro:
|
shiro:
|
||||||
|
|
Loading…
Reference in New Issue