From b955ff95338c906899a2e2644647eea38c0effd6 Mon Sep 17 00:00:00 2001 From: byung eun park Date: Mon, 16 Sep 2019 17:09:32 +0900 Subject: [PATCH] file controller added --- .../controller/FileUploadController.java | 70 +++++++++++++++++++ .../data/exception/FileDownloadException.java | 11 +++ .../data/exception/FileUploadException.java | 11 +++ .../data/payload/FileUploadResponse.java | 17 +++++ .../service/FileUploadDownloadService.java | 67 ++++++++++++++++++ .../site/controller/BankInfoController.java | 8 +++ .../modules/site/service/BankInfoService.java | 8 +++ src/main/resources/application.yml | 12 ++++ 8 files changed, 204 insertions(+) create mode 100644 src/main/java/com/totopia/server/commons/controller/FileUploadController.java create mode 100644 src/main/java/com/totopia/server/commons/data/exception/FileDownloadException.java create mode 100644 src/main/java/com/totopia/server/commons/data/exception/FileUploadException.java create mode 100644 src/main/java/com/totopia/server/commons/data/payload/FileUploadResponse.java create mode 100644 src/main/java/com/totopia/server/commons/data/service/FileUploadDownloadService.java diff --git a/src/main/java/com/totopia/server/commons/controller/FileUploadController.java b/src/main/java/com/totopia/server/commons/controller/FileUploadController.java new file mode 100644 index 0000000..714efb4 --- /dev/null +++ b/src/main/java/com/totopia/server/commons/controller/FileUploadController.java @@ -0,0 +1,70 @@ +package com.totopia.server.commons.controller; + +import com.totopia.server.commons.data.payload.FileUploadResponse; +import com.totopia.server.commons.data.service.FileUploadDownloadService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@Slf4j +public class FileUploadController { + @Autowired + private FileUploadDownloadService fileService; + + @PostMapping("/uploadFile") + public FileUploadResponse uploadFile(@RequestParam("file") MultipartFile file) { + String fileName = fileService.storeFile(file); + + String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() + .path("/downloadFile/") + .path(fileName) + .toUriString(); + + return new FileUploadResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize()); + } + + @PostMapping("/uploadMultipleFiles") + public List uploadMultipleFiles(@RequestParam("files") MultipartFile[] files){ + return Arrays.asList(files) + .stream() + .map(file -> uploadFile(file)) + .collect(Collectors.toList()); + } + + @GetMapping("/downloadFile/{fileName:.+}") + public ResponseEntity downloadFile(@PathVariable String fileName, HttpServletRequest request){ + // Load file as Resource + Resource resource = fileService.loadFileAsResource(fileName); + + // Try to determine file's content type + String contentType = null; + try { + contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath()); + } catch (IOException ex) { + log.info("Could not determine file type."); + } + + // Fallback to the default content type if type could not be determined + if(contentType == null) { + contentType = "application/octet-stream"; + } + + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType(contentType)) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") + .body(resource); + } +} diff --git a/src/main/java/com/totopia/server/commons/data/exception/FileDownloadException.java b/src/main/java/com/totopia/server/commons/data/exception/FileDownloadException.java new file mode 100644 index 0000000..afc15c5 --- /dev/null +++ b/src/main/java/com/totopia/server/commons/data/exception/FileDownloadException.java @@ -0,0 +1,11 @@ +package com.totopia.server.commons.data.exception; + +public class FileDownloadException extends RuntimeException { + public FileDownloadException(String message) { + super(message); + } + + public FileDownloadException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/totopia/server/commons/data/exception/FileUploadException.java b/src/main/java/com/totopia/server/commons/data/exception/FileUploadException.java new file mode 100644 index 0000000..6432340 --- /dev/null +++ b/src/main/java/com/totopia/server/commons/data/exception/FileUploadException.java @@ -0,0 +1,11 @@ +package com.totopia.server.commons.data.exception; + +public class FileUploadException extends RuntimeException { + public FileUploadException(String message) { + super(message); + } + + public FileUploadException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/totopia/server/commons/data/payload/FileUploadResponse.java b/src/main/java/com/totopia/server/commons/data/payload/FileUploadResponse.java new file mode 100644 index 0000000..bb264bd --- /dev/null +++ b/src/main/java/com/totopia/server/commons/data/payload/FileUploadResponse.java @@ -0,0 +1,17 @@ +package com.totopia.server.commons.data.payload; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class FileUploadResponse { + private String fileName; + private String fileDownloadUri; + private String fileType; + private Long size; +} diff --git a/src/main/java/com/totopia/server/commons/data/service/FileUploadDownloadService.java b/src/main/java/com/totopia/server/commons/data/service/FileUploadDownloadService.java new file mode 100644 index 0000000..4d078b3 --- /dev/null +++ b/src/main/java/com/totopia/server/commons/data/service/FileUploadDownloadService.java @@ -0,0 +1,67 @@ +package com.totopia.server.commons.data.service; + +import com.totopia.server.commons.data.exception.FileDownloadException; +import com.totopia.server.commons.data.exception.FileUploadException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.core.io.UrlResource; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.net.MalformedURLException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +@Service +public class FileUploadDownloadService { + + private final Path fileLocation; + + @Autowired + public FileUploadDownloadService() { + this.fileLocation = Paths.get("/home/geek/Downloads/Temp_Upload").toAbsolutePath().normalize(); + + try { + Files.createDirectories(this.fileLocation); + } catch (Exception e) { + throw new FileUploadException("파일을 업로드할 디렉토리를 생성하지 못했습니다.", e); + } + } + + public String storeFile(MultipartFile file) { + String fileName = StringUtils.cleanPath((file.getOriginalFilename())); + + try { + if(fileName.contains("..")) + throw new FileUploadException("파일명에 부적합 문자가 포함되어 있습니다. " + fileName); + + Path targetLocation = this.fileLocation.resolve(fileName); + + Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING); + + return fileName; + } catch (Exception e) { + throw new FileUploadException("["+fileName+"] 파일 업로드에 실패하였습니다. 다시 시도하십시오.",e); + } + } + + public Resource loadFileAsResource(String fileName) { + try { + Path filePath = this.fileLocation.resolve(fileName).normalize(); + Resource resource = new UrlResource(filePath.toUri()); + + if(resource.exists()) { + return resource; + }else { + throw new FileDownloadException(fileName + " 파일을 찾을 수 없습니다."); + } + }catch(MalformedURLException e) { + throw new FileDownloadException(fileName + " 파일을 찾을 수 없습니다.", e); + } + } + + +} diff --git a/src/main/java/com/totopia/server/modules/site/controller/BankInfoController.java b/src/main/java/com/totopia/server/modules/site/controller/BankInfoController.java index 2f816b8..ca25753 100644 --- a/src/main/java/com/totopia/server/modules/site/controller/BankInfoController.java +++ b/src/main/java/com/totopia/server/modules/site/controller/BankInfoController.java @@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; @RestController public class BankInfoController { @@ -31,6 +32,13 @@ public class BankInfoController { return this.bankInfoService.getAllBankInfo(pageable); } + @GetMapping(value = "/bank_info/test") + public List allTest() { + + return this.bankInfoService.getAllBankInfoTest(); + } + + @PostMapping(value = "/bank_info") @ResponseStatus(code = HttpStatus.CREATED) public ResponseEntity save(@Valid @RequestBody BankInfoEntity bankInfoEntity) throws Exception{ diff --git a/src/main/java/com/totopia/server/modules/site/service/BankInfoService.java b/src/main/java/com/totopia/server/modules/site/service/BankInfoService.java index d47b1b2..0775bd0 100644 --- a/src/main/java/com/totopia/server/modules/site/service/BankInfoService.java +++ b/src/main/java/com/totopia/server/modules/site/service/BankInfoService.java @@ -9,6 +9,8 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import java.util.List; + @Service @Slf4j public class BankInfoService { @@ -40,6 +42,12 @@ public class BankInfoService { return this.bankInfoRepository.findAll(pageable); } + public List getAllBankInfoTest() { + + + return this.bankInfoRepository.findAll(); + } + public BankInfoEntity getBankInfoById(Short bankInfoId) { return this.bankInfoRepository.findById(bankInfoId).orElseThrow(null); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f4e4f52..9c7211f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -39,6 +39,16 @@ spring: serialization: WRITE_DATES_AS_TIMESTAMPS: false time-zone: UTC + #Multipart properties + servlet: + multipart: + enabled: true + max-file-size: 10MB + max-request-size: 10MB + +#file upload dir +file: +# upload-dir=/Users/geek/Download/Temp_Upload # Logger configuration logging: @@ -64,3 +74,5 @@ app: email.verification.duration=3600000 password.reset.duration=3600000 refresh.duration=2592000000 + +