diff --git a/build.gradle b/build.gradle index 69fe1d3..d29ca65 100644 --- a/build.gradle +++ b/build.gradle @@ -221,6 +221,7 @@ dependencies { implementation 'org.apache.httpcomponents:httpcore:4.4.15' implementation 'commons-collections:commons-collections:3.2.2' implementation 'commons-fileupload:commons-fileupload:1.4' + implementation 'commons-io:commons-io:2.11.0' implementation 'javax.mail:mail:1.4.7' implementation 'org.codehaus.jackson:jackson-core-asl:1.9.13' diff --git a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java index 8e75eb7..fa76bea 100644 --- a/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java +++ b/src/main/java/com/xit/biz/ctgy/controller/CtgyFileMgtController.java @@ -3,6 +3,7 @@ package com.xit.biz.ctgy.controller; import com.xit.biz.ctgy.dto.MinInfoBoard680Dto; import com.xit.biz.ctgy.entity.MinInfoBoard680; +import com.xit.biz.ctgy.entity.MinSimsa680; import com.xit.biz.ctgy.service.ICtgyFileService; import com.xit.core.api.IRestResponse; import com.xit.core.api.RestResponse; @@ -11,6 +12,9 @@ import com.xit.core.util.Checks; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import org.apache.commons.io.FileUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -18,6 +22,12 @@ import org.springframework.lang.NonNull; import org.springframework.web.bind.annotation.*; import javax.annotation.Nonnull; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.List; @Tag(name = "CtgyFileMgtController", description = "파일 관리") @@ -25,13 +35,25 @@ import java.util.List; @RequiredArgsConstructor @RequestMapping("/api/v1/ctgy/file") public class CtgyFileMgtController { - private final ICtgyFileService fileService; + + private final Environment env; + + @Value("${file.cmm.upload.root:c:/data/file/upload}") + private String rootPath; + + @Value("${file.cmm.upload.path:/kangnamSIM/simUpFile/}") + private String uploadPath; + + @Value("${file.cmm.upload.url}") + private String serviceUrl; + + private final ICtgyFileService service; @Operation(summary = "파일 조회" , description = "등록된 파일 조회") @GetMapping("/{id}") public ResponseEntity findFiles(@PathVariable("inCode") @NonNull final Long inCode) { AssertUtils.isTrue(!Checks.isEmpty(inCode), "대상 게시글[inCode]을 선택해 주세요."); - return RestResponse.of(fileService.findFiles(inCode)); + return RestResponse.of(service.findFiles(inCode)); } @Operation(summary = "공지사항 저장" , description = "공지사항 저장") @@ -46,15 +68,40 @@ public class CtgyFileMgtController { .inContents(dto.getInContents()) .inFilename(dto.getInFilename()) .build(); - return RestResponse.of(fileService.saveFiles(minInfoBoard680, dto.getFiles())); + return RestResponse.of(service.saveFiles(minInfoBoard680, dto.getFiles())); } @Operation(summary = "공지사항 삭제" , description = "공지사항 삭제") @PostMapping(value="/pboard/{inCode}") public ResponseEntity removePublicBoardFile(@PathVariable @Nonnull final Long inCode) { AssertUtils.isTrue(!Checks.isEmpty(inCode), "공지 사항이 선택되지 않았습니다."); - fileService.removePublicBoardFile(inCode); + service.removePublicBoardFile(inCode); return RestResponse.of(HttpStatus.OK); } + + @GetMapping("/download/{inCode}") + public void download(@PathVariable Long inCode, HttpServletResponse response) throws IOException { + + MinInfoBoard680 entity = service.findFiles(inCode); + + String absFile = ""; + + if(Arrays.asList(env.getActiveProfiles()).contains("prod")) + absFile = entity.getInFileurl() + File.separator + entity.getInFilename(); + else + absFile = rootPath + entity.getInFileurl().split(serviceUrl)[1] + File.separator + entity.getInFilename(); + + + byte[] fileByte = FileUtils.readFileToByteArray(new File(absFile)); + + response.setContentType("application/octet-stream"); + response.setHeader("Content-Disposition", "attachment; fileName=\"" + URLEncoder.encode(entity.getInFilename(), StandardCharsets.UTF_8)+"\";"); + response.setHeader("Content-Transfer-Encoding", "binary"); + response.setHeader("Content-Length", String.valueOf(entity.getInFilesize())); + + response.getOutputStream().write(fileByte); + response.getOutputStream().flush(); + response.getOutputStream().close(); + } } diff --git a/src/main/resources/config/conf.yml b/src/main/resources/config/conf.yml index 4db8931..176e0c4 100644 --- a/src/main/resources/config/conf.yml +++ b/src/main/resources/config/conf.yml @@ -17,7 +17,8 @@ api: file: cmm: upload: - root: c:/data/file/upload + #root: c:/data/file/upload + root: /Users/minuk/data/file/upload # 공지사항 path: /kangnamSIM/simUpFile url: http://traffic.gangnam.go.kr