From 74d0031c16c468fa82208dbcdde4fa3766a58eeb Mon Sep 17 00:00:00 2001
From: rain <167982779@qq.com>
Date: Thu, 09 May 2024 16:30:55 +0800
Subject: [PATCH] 完成成果数据入库(DB)方式,修改DB文件上传接口。
---
src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java | 44 ++++++++++++++++++++++----------------------
1 files changed, 22 insertions(+), 22 deletions(-)
diff --git a/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java b/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
index f35d765..48dc95f 100644
--- a/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
+++ b/src/main/java/com/dji/sample/territory/service/impl/TbDkjbxxServiceImpl.java
@@ -33,6 +33,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
+import java.util.UUID;
import static com.dji.sample.patches.utils.MultipartFileTOFileUtil.convert;
import static com.dji.sample.patches.utils.ZipUtil.zipFolder;
@@ -75,7 +76,7 @@
// 获取上传的文件输入流
InputStream inputStream = file.getInputStream();
// 创建输出流,将文件内容写入资源文件
- OutputStream outputStream = new FileOutputStream(territoryConfigPojo.getPath());
+ OutputStream outputStream = new FileOutputStream(territoryConfigPojo.getResource());
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
@@ -121,30 +122,29 @@
if (url.isEmpty()) {
throw new IllegalArgumentException("上传文件为空");
}
- downloadFile(url);
+ String saveFilePath = territoryConfigPojo.getResource(); // 要保存文件的本地路径
+
+ try {
+ downloadFile(url, saveFilePath);
+ System.out.println("文件下载完成");
+ } catch (IOException e) {
+ System.out.println("下载文件时发生错误:" + e.getMessage());
+ }
return ResponseResult.success();
}
- public File downloadFile(String fileUrl) {
- File downloadedFile = null;
- try {
- URL url = new URL(fileUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- InputStream inputStream = connection.getInputStream();
- downloadedFile = new File(territoryConfigPojo.getPath());
- OutputStream outputStream = new FileOutputStream(downloadedFile);
- byte[] buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- inputStream.close();
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ public static void downloadFile(String fileUrl, String saveFilePath) throws IOException {
+ // 创建URL对象
+ URL url = new URL(fileUrl);
- return downloadedFile;
+ // 打开连接
+ try (BufferedInputStream in = new BufferedInputStream(url.openStream());
+ FileOutputStream fileOutputStream = new FileOutputStream(saveFilePath)) {
+ byte[] dataBuffer = new byte[1024];
+ int bytesRead;
+ while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
+ fileOutputStream.write(dataBuffer, 0, bytesRead);
+ }
+ }
}
/**
--
Gitblit v1.9.3