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