fix[project]: 同步待办时,下载待办的报告文件
This commit is contained in:
@@ -586,75 +586,75 @@ public class FilesUtil {
|
||||
|
||||
/**
|
||||
* 从指定URL下载文件到本地
|
||||
* @param fileUrl 远程文件的URL地址
|
||||
* @param fileUrlList 远程文件的URL地址
|
||||
* @param savePath 本地保存路径(包含文件名,例如:/opt/demand/report/有限元仿真结果报告.pdf)
|
||||
* @throws IOException 处理IO异常
|
||||
*/
|
||||
public static void downloadFile(String fileUrl, String savePath) throws IOException {
|
||||
|
||||
public static void downloadFile(List<String> fileUrlList, String savePath) throws IOException {
|
||||
createDirectoryIfNotExists(savePath);
|
||||
for (String fileUrl : fileUrlList) {
|
||||
// 声明连接和流对象
|
||||
HttpURLConnection connection = null;
|
||||
BufferedInputStream in = null;
|
||||
BufferedOutputStream out = null;
|
||||
|
||||
// 声明连接和流对象
|
||||
HttpURLConnection connection = null;
|
||||
BufferedInputStream in = null;
|
||||
BufferedOutputStream out = null;
|
||||
try {
|
||||
// 1. 创建URL对象
|
||||
URL url = new URL(fileUrl);
|
||||
|
||||
try {
|
||||
// 1. 创建URL对象
|
||||
URL url = new URL(fileUrl);
|
||||
// 2. 打开连接并设置属性
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
// 设置连接超时时间(5秒)
|
||||
connection.setConnectTimeout(5000);
|
||||
// 设置读取超时时间(10秒)
|
||||
connection.setReadTimeout(10000);
|
||||
// 设置请求方法
|
||||
connection.setRequestMethod("GET");
|
||||
// 允许输入流
|
||||
connection.setDoInput(true);
|
||||
|
||||
// 2. 打开连接并设置属性
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
// 设置连接超时时间(5秒)
|
||||
connection.setConnectTimeout(5000);
|
||||
// 设置读取超时时间(10秒)
|
||||
connection.setReadTimeout(10000);
|
||||
// 设置请求方法
|
||||
connection.setRequestMethod("GET");
|
||||
// 允许输入流
|
||||
connection.setDoInput(true);
|
||||
|
||||
// 3. 检查响应码,200表示成功
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
throw new IOException("下载失败,HTTP响应码:" + responseCode);
|
||||
}
|
||||
|
||||
// 4. 获取文件大小(用于进度显示)
|
||||
long fileSize = connection.getContentLengthLong();
|
||||
log.info("文件大小:{}", formatFileSize(fileSize));
|
||||
|
||||
// 5. 创建输入输出流
|
||||
in = new BufferedInputStream(connection.getInputStream());
|
||||
out = new BufferedOutputStream(new FileOutputStream(savePath));
|
||||
|
||||
// 6. 缓冲区读写数据
|
||||
byte[] buffer = new byte[4096]; // 4KB缓冲区
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
log.info("文件下载完成!保存路径:{}",savePath);
|
||||
|
||||
} finally {
|
||||
// 7. 关闭所有流和连接,确保资源释放
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// 3. 检查响应码,200表示成功
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
throw new IOException("下载失败,HTTP响应码:" + responseCode);
|
||||
}
|
||||
}
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
// 4. 获取文件大小(用于进度显示)
|
||||
long fileSize = connection.getContentLengthLong();
|
||||
log.info("文件大小:{}", formatFileSize(fileSize));
|
||||
|
||||
// 5. 创建输入输出流
|
||||
in = new BufferedInputStream(connection.getInputStream());
|
||||
out = new BufferedOutputStream(new FileOutputStream(savePath));
|
||||
|
||||
// 6. 缓冲区读写数据
|
||||
byte[] buffer = new byte[4096]; // 4KB缓冲区
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
log.info("文件下载完成!保存路径:{}",savePath);
|
||||
|
||||
} finally {
|
||||
// 7. 关闭所有流和连接,确保资源释放
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user