From 614fae372658ad1fbf75ded309e4b414cd25c3cc Mon Sep 17 00:00:00 2001 From: yennanliu Date: Thu, 23 Nov 2023 16:16:24 +0800 Subject: [PATCH 1/2] add report path --- .../src/main/resources/report/20231123-16-16-09_report.json | 1 + 1 file changed, 1 insertion(+) create mode 100644 springWarehouse/src/main/resources/report/20231123-16-16-09_report.json diff --git a/springWarehouse/src/main/resources/report/20231123-16-16-09_report.json b/springWarehouse/src/main/resources/report/20231123-16-16-09_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231123-16-16-09_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file From 040757258ba8de134861624ffe6d8b57dcfaaaa1 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Thu, 23 Nov 2023 17:07:07 +0800 Subject: [PATCH 2/2] hardcode download path, add test, dummy file --- .../springWarehouse/WarehouseApplication.java | 8 ++++---- .../controller/DownloadController.java | 18 ++++++++++++----- .../src/main/resources/demo_report.json | 1 + .../controller/DownloadControllerTest.java | 20 +++++++++++++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 springWarehouse/src/main/resources/demo_report.json diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/WarehouseApplication.java b/springWarehouse/src/main/java/com/yen/springWarehouse/WarehouseApplication.java index cbf320879..288ecc161 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/WarehouseApplication.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/WarehouseApplication.java @@ -11,9 +11,9 @@ @SpringBootApplication public class WarehouseApplication { - public static void main(String[] args) { + public static void main(String[] args) { - SpringApplication.run(WarehouseApplication.class, args); - } + SpringApplication.run(WarehouseApplication.class, args); + } -} +} \ No newline at end of file diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java index d32773f3c..2e3ebe1f2 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java @@ -39,7 +39,8 @@ public class DownloadController { DownloadStatusMapper downloadStatusMapper; String userDirectory = new File("").getAbsolutePath(); - final String prefix = userDirectory + "/src/main/resources/report/"; + //final String prefix = userDirectory + "/src/main/resources/report/"; + final String prefix = userDirectory + "/src/main/resources/"; @GetMapping("/list") public String list(Map map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) { @@ -77,6 +78,7 @@ public String createDownload() { map.put("age", 17); // save file to local // TODO : save it to remote file system (e.g. S3) Boolean result = fileUtil.writeJsonFile(map, prefix + fileName); + //Boolean result = fileUtil.writeJsonFile(map, fileName); if (result) { DownloadStatus downloadStatus = new DownloadStatus(); downloadStatus.setStatus("completed"); @@ -97,10 +99,16 @@ public ResponseEntity downloadFile(String url) throws IOException { log.info(">>> (ResponseEntity downloadFile) url = " + url); //List downloadStatusList = downloadStatusMapper.getAllDownloadStatus(); - //String downloadUrl = "/report/" + downloadStatusList.get(0).getDownloadUrl(); - String downloadUrl = "/report/" + url; - log.info("downloadUrl = " + downloadUrl); - Resource resource = new ClassPathResource(downloadUrl); + String downloadUrl = url; + log.info(">>> downloadUrl = " + downloadUrl); + + // TODO : fix why can't read file from downloadUrl + //ClassPathResource resource = new ClassPathResource(String.valueOf(downloadUrl)); + ClassPathResource resource = new ClassPathResource("demo_report.json"); + log.info(resource.getPath()); + log.info(String.valueOf(resource.exists())); + log.info(String.valueOf(resource.getURL())); + HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=%s".format("test.json")); diff --git a/springWarehouse/src/main/resources/demo_report.json b/springWarehouse/src/main/resources/demo_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/demo_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/test/java/com/yen/springWarehouse/controller/DownloadControllerTest.java b/springWarehouse/src/test/java/com/yen/springWarehouse/controller/DownloadControllerTest.java index f0b28b9cc..e2b600ee0 100644 --- a/springWarehouse/src/test/java/com/yen/springWarehouse/controller/DownloadControllerTest.java +++ b/springWarehouse/src/test/java/com/yen/springWarehouse/controller/DownloadControllerTest.java @@ -2,7 +2,10 @@ import com.yen.springWarehouse.util.DateTimeUtils; import org.junit.jupiter.api.Test; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import java.io.IOException; import java.text.SimpleDateFormat; import static org.junit.jupiter.api.Assertions.*; @@ -21,4 +24,21 @@ public void testPrepareDownloadFile(){ System.out.println(dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS()); } + @Test + public void testGetResourcePath() throws IOException { + + ClassPathResource r1 = new ClassPathResource("application.properties"); + System.out.println(r1.getPath()); + System.out.println(r1.exists()); + System.out.println(r1.getURL()); + + //ClassPathResource r2 = new ClassPathResource("/report/20231123-16-16-09_report.json"); + ClassPathResource r2 = new ClassPathResource("20231123-16-52-30_report.json"); + System.out.println(r2.getPath()); + System.out.println(r2.exists()); + System.out.println(r2.getURL()); + + + } + } \ No newline at end of file