개요
txt 파일을 생성한 뒤 RestTemplate을 통해 파일 인스턴스로 보내야 하는데 MultipartFile 객체로 변환해서 보내야 했기 때문에 간단하게 함수를 작성해봤습니다.
소스 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private MultipartFile convertFileToMultipartFile(File file) throws IOException { | |
FileItem fileItem = new DiskFileItem("file" | |
, Files.probeContentType(file.toPath()) | |
, false, file.getName() | |
, (int) file.length(), | |
file.getParentFile()); | |
try { | |
InputStream is = new FileInputStream(file); | |
OutputStream os = fileItem.getOutputStream(); | |
IOUtils.copy(is, os); | |
} catch (IOException e) { | |
log.error("[convertFileToMultipartFile] error {}", e.getMessage()); | |
throw new IOException(e); | |
} | |
return new CommonsMultipartFile(fileItem); | |
} |
반응형
'[DEV] 기록' 카테고리의 다른 글
[SpringBoot + MyBatis] TypeHandler 통해 JSON List varchar로 저장 (0) | 2022.04.13 |
---|---|
[SpringBoot] 여러 파일을 .zip 파일로 압축해서 다운로드 (0) | 2022.04.12 |
[SpringBoot + JPA] @NamedEntityGraph (0) | 2022.04.10 |
[SpringBoot] git에 application.properties 안 올리는 방법 (0) | 2022.04.10 |
[PostgreSQL] PostgreSQL 비밀번호 잊어버렸을 경우 (0) | 2022.04.09 |