Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockMultipartFile cannot be resolved to a type

I am writing code for a spring program, and I need to convert a File object to a MultipartFile object. I am trying to do this by using the MockMultipartFile class:

FileInputStream input = new FileInputStream(toUpload);
MultipartFile multipartFile = new MockMultipartFile("file",
                toUpload.getName(), "text/json", IOUtils.toByteArray(input));

However, I keep getting the error MockMultipartFile cannot be resolved to a type. Why is this? If it helps, I'm using Maven, in case it may be a dependency error that I don't know about.

I also tried to do this using CommonsMultipartFile. That code is here (toUpload is the File object I am trying to convert):

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

When I try this method, I get the error The constructor CommonsMultipartFile(DiskFileItem) is undefined. Why is that?

EDIT: Here is our pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-uploading-files</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
like image 441
Cameron Payton Avatar asked Sep 21 '25 01:09

Cameron Payton


1 Answers

To use spring's MockMultipartFile you need to add spring-test as a compile dependency (which would be unusual, since it is generally used as a test dependency):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
</dependency>
like image 139
Roman Avatar answered Sep 22 '25 16:09

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!