简体   繁体   中英

Converting File to MultipartFile using CommonsMultipartFile

I'm trying to make a POST request with a MultipartFile. First I want to convert a File into a MultipartFile:

File file = new File(filePath);
DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file.getName(), (int) file.length(), file.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

I'm getting this message under CommonsMultipartFile:

The type org.apache.commons.fileupload.FileItem cannot be resolved. It is indirectly referenced from required .class files

I cannot figure out how to fix that.

The error is telling you that you are missing a required library.

You should add commons-fileupload in your classpath. If you are using Maven, add this dependency to your POM:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

If not, just download the JAR and add it manually to your classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM