새소식

Program Language/☕ JAVA

JAVA Converting File to MultiPartFile

  • -
728x90

 

Good moring or afternoon sir.

Today, I'm sharing you how to convert [File to MultiPartFile] in JAVA.

Trust Me! This is the code I'm actually using in my system :)

 

1. You can use CommonsMultipartFile

An example of a filePath is shown below.

ex) C:/Users/han/test.xlsx

File yourFile = new File(filePath);
DiskFileItem fileItem = new DiskFileItem("file", Files.probeContentType(yourFile.toPath()), false, yourFile.getName(), (int) yourFile.length() , yourFile.getParentFile());
InputStream input = new FileInputStream(yourFile);
OutputStream os = fileItem.getOutputStream();
IOUtils.copy(input, os);
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

 

I hope this is the result you want.

If it's not working or if you have any questions, please leave a comment.

Have a nice day!

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.