티스토리 뷰

File과 관련된 백엔드단 테스트가 필요한 경우가 종종 있다. File을 MultipartFile 형식으로 바꾸어서 처리를 해줘야 하는 경우가 있는데 어떻게 해야 하는지 난감하다. 그럴때는 아래와 같은 로직을 추가함으로써 MultipartFile을 얻을 수 있다. 

File file = new File("C:\\temp\\test.xlsx");
    	
DiskFileItem fileItem = new DiskFileItem("file", Files.probeContentType(file.toPath()), false, file.getName(), (int) file.length() , file.getParentFile());
    		       
InputStream input = new FileInputStream(file);
OutputStream os = fileItem.getOutputStream();
IOUtils.copy(input, os);
    		        
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

 

MultipartFile to File과 같은 반대의 상황은 어떻게 해야 하나? 다음과 같이 하면 된다.

File file = new File(multipartFile.getOrigianlFilename());
multipartFile.transferTo(file);

위와 같이 간단히 File을 얻을 수 있다. 

 

끝!

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
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 31