[Spring]/springboot
[SpringBoot] RestAPI 파일업로드 - 2
미나미나미
2022. 2. 24. 23:09
2022.02.24 - [[Spring]/springboot] - [SpringBoot] RestAPI 파일업로드 - 1
[SpringBoot] RestAPI 파일업로드 - 1
1. start.spring.io에서 스프링 프로젝트 생성하기 2. 파일 업로드 관련 properties 생성 spring: servlet: multipart: max-request-size: 500MB # request 요청 제한(defalut 10mb) max-file-size: 500MB # 파..
minaminaworld.tistory.com
1. 프로젝트 메인에서 업로드 폴더 생성

@Value("${uploadFolder}")
private String uploadFolder;
public static void main(String[] args) {
SpringApplication.run(TistoryFileDownloadApplication.class, args);
}
@PostConstruct
public void createUploadFolder() {
Path upload = Paths.get(uploadFolder);
try {
if (!Files.exists(upload)) {
Files.createDirectory(upload);
}
} catch (IOException e) {
e.printStackTrace();
}
}
2. 파일 저장 서비스 코드 작성

@Service
public class FileService {
@Value("${uploadFolder}")
private String uploadFolder;
public void save(MultipartFile file) {
Path upload = Paths.get(uploadFolder);
try {
Files.copy(file.getInputStream(), upload.resolve(file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. Postman 테스트 결과
