일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- banner
- 카멜케이스
- Entity
- linux
- nginx
- 자바란
- 2중for
- spring-rest-docs
- javax
- pdf변환
- redis
- OS
- MongoTemplate
- 스네이크케이스
- java
- commons-fileupload
- Docker
- springboot
- 태그없음
- inmemory
- mongodb
- 외부허용
- for
- spring
- 파스칼케이스
- websokcet
- jpa
- 테이블정의서
- 케밥케이스
- 반복
Archives
- Today
- Total
겸쨔
[spring] apache commons-fileupload MultipartFile 변환 본문
반응형
안녕하세요.
비즈니스 로직 단에서 생성한 파일을 MultipartFile로 변환해야 하는 일이 있었어요.
구글링하다가 찾은건데 apache에서 만든 commons-fileupload 라이브러리를 사용하면,
쉽게 가능하다해서 사용한거 기록할겸 작성해요.
환경
- jdk 11
- spring boot 2.7.0
- maven
의존성
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
우선 html body에 컨텐츠만 넣고나서 바로 업로드 할거에요.
Document doc = Jsoup.parse("<html></html>");
doc.body().append(mcArticle.getConts());
String htmlContent = doc.html();
byte[] contentBytes = htmlContent.getBytes(StandardCharsets.UTF_8);
DiskFileItem fileItem = new DiskFileItem("file", "text/html", false, "temp.html", contentBytes.length, null);
try (OutputStream os = fileItem.getOutputStream()) {
IOUtils.write(contentBytes, os);
}
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
저는 html 파일만 변환 했는데요.
코드 보면 다른 파일들도 변환 될거 같네요 ~
반응형
'spring' 카테고리의 다른 글
[spring] 실행 시 출력 되는 배너 커스텀 (0) | 2024.07.20 |
---|