file controller added
This commit is contained in:
parent
5a1797add8
commit
b955ff9533
|
@ -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<FileUploadResponse> uploadMultipleFiles(@RequestParam("files") MultipartFile[] files){
|
||||||
|
return Arrays.asList(files)
|
||||||
|
.stream()
|
||||||
|
.map(file -> uploadFile(file))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/downloadFile/{fileName:.+}")
|
||||||
|
public ResponseEntity<Resource> 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class BankInfoController {
|
public class BankInfoController {
|
||||||
|
@ -31,6 +32,13 @@ public class BankInfoController {
|
||||||
return this.bankInfoService.getAllBankInfo(pageable);
|
return this.bankInfoService.getAllBankInfo(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = "/bank_info/test")
|
||||||
|
public List<BankInfoEntity> allTest() {
|
||||||
|
|
||||||
|
return this.bankInfoService.getAllBankInfoTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping(value = "/bank_info")
|
@PostMapping(value = "/bank_info")
|
||||||
@ResponseStatus(code = HttpStatus.CREATED)
|
@ResponseStatus(code = HttpStatus.CREATED)
|
||||||
public ResponseEntity<?> save(@Valid @RequestBody BankInfoEntity bankInfoEntity) throws Exception{
|
public ResponseEntity<?> save(@Valid @RequestBody BankInfoEntity bankInfoEntity) throws Exception{
|
||||||
|
|
|
@ -9,6 +9,8 @@ import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class BankInfoService {
|
public class BankInfoService {
|
||||||
|
@ -40,6 +42,12 @@ public class BankInfoService {
|
||||||
return this.bankInfoRepository.findAll(pageable);
|
return this.bankInfoRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<BankInfoEntity> getAllBankInfoTest() {
|
||||||
|
|
||||||
|
|
||||||
|
return this.bankInfoRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
public BankInfoEntity getBankInfoById(Short bankInfoId) {
|
public BankInfoEntity getBankInfoById(Short bankInfoId) {
|
||||||
return this.bankInfoRepository.findById(bankInfoId).orElseThrow(null);
|
return this.bankInfoRepository.findById(bankInfoId).orElseThrow(null);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,16 @@ spring:
|
||||||
serialization:
|
serialization:
|
||||||
WRITE_DATES_AS_TIMESTAMPS: false
|
WRITE_DATES_AS_TIMESTAMPS: false
|
||||||
time-zone: UTC
|
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
|
# Logger configuration
|
||||||
logging:
|
logging:
|
||||||
|
@ -64,3 +74,5 @@ app:
|
||||||
email.verification.duration=3600000
|
email.verification.duration=3600000
|
||||||
password.reset.duration=3600000
|
password.reset.duration=3600000
|
||||||
refresh.duration=2592000000
|
refresh.duration=2592000000
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user