Skip to content

Commit

Permalink
fix file save path, fix html,
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 10, 2023
1 parent 114ce14 commit 239744a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Binary file added 20231110-18-48-52_report.csv
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.yen.springWarehouse.bean.DownloadStatus;
import com.yen.springWarehouse.service.DownloadStatusService;
import com.yen.springWarehouse.util.DateTimeUtils;
import com.yen.springWarehouse.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -57,12 +58,23 @@ public String createDownload(){

DateTimeUtils dateTimeUtils = new DateTimeUtils();
String timestamp = dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS();

FileUtil fileUtil = new FileUtil();

final String prefix = "/Users/yennanliu/SpringPlayground/";
String fileName = "/" + timestamp + "_report.csv";
DownloadStatus downloadStatus = new DownloadStatus();
downloadStatus.setStatus("completed");
downloadStatus.setDownloadUrl(fileName);
downloadStatus.setCreateTime(new Date());
downloadStatusService.save(downloadStatus);
// save file to local // TODO : save it to remote file system (e.g. S3)
if (fileUtil.writeFile("some data", prefix+fileName)){
log.info("save File OK");
// save to DB
downloadStatusService.save(downloadStatus);
}else {
log.info("save File failed");
}
return "download/success";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yen.springWarehouse.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;

@Slf4j
public class FileUtil {

public Boolean writeFile(String intputString, String fileName){

try{
String value = intputString; //"Hello";
FileOutputStream fos = new FileOutputStream(fileName);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(value);
outStream.close();
return true;
}catch (Exception e){
log.error("writeFile error : " + e);
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
<body style="padding:8px;">
<h3 class="title">Report Download</h3>

<button class="create" th:href="@{/download/create}" >Create Download 1</button>
<button class="create" th:onclick="'window.location.href=' + @{/download/create}">Create Download 2</button>
<a href="/download/create">
<button class="create">Create Download 3</button>
<button class="create">Create Download</button>
</a>


Expand Down

0 comments on commit 239744a

Please sign in to comment.