3

We were creating a upload call by using the spring boot multipart (Maven).

Our pom.xml file

    <?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>com.awsarTest</groupId>
    <artifactId>awsarTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>awsarTest Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.21.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.5.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>CWSAuthentication</groupId>
            <artifactId>CWSAuthentication</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>Authentication</groupId>
            <artifactId>Authentication</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.ws</groupId>
            <artifactId>jakarta.xml.ws-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.3.3</version> <!-- Use the latest version -->
        </dependency>
        <!-- Apache Commons FileUpload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- Apache Commons IO -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>awsarTest</finalName>
        <pluginManagement>
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

And springapplicationconfig.java`

package awsarTest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@ComponentScan(basePackages = "awsarTest.controller")
@Configuration
@EnableWebMvc
public class SpringApplicationConfig {

    
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver vw = new InternalResourceViewResolver();
        vw.setPrefix("/WEB-INF/jsp/");;
        vw.setSuffix(".jsp");;
        return vw;
    }
    
    
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSizePerFile(5242880); // Set your max file size limit if needed
        return resolver;
    }
}

Application.porperties files:

# MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB

And controller

package awsarTest.controller;

import com.opentext.livelink.service.core.Authentication;
import com.opentext.livelink.service.core.Authentication_Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.soap.SOAPFaultException;

@RestController
@RequestMapping("/auth")
public class AuthControllerTest {

    @RequestMapping(value = "/authenticate")
    public String authenticateUser(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password ) {
        
        //return "Hello, " + name + "!";
        String USERNAME = username;
        String PASSWORD = password;

        // Create the Authentication service client
        Authentication_Service authService = new Authentication_Service();
        Authentication authClient = authService.getBasicHttpBindingAuthentication();

        // Store the authentication token
        String authToken;

        // Call the AuthenticateUser() method to get an authentication token
        try {
            System.out.print("Authenticating User...");
            authToken = authClient.authenticateUser(USERNAME, PASSWORD);
            System.out.println("SUCCESS!\nAuthentication Token: " + authToken);
        } catch (SOAPFaultException e) {
            System.out.println("FAILED!\n");
            System.out.println(e.getFault().getFaultCode() + " : " + e.getMessage());
            return "Authentication failed";
        }

        // You can do further processing or return the token as needed
        return "Authentication Token: " + authToken;
    }
    
    **@PostMapping(value = "/upload")
    public String uplaoddocument(@RequestParam("file") MultipartFile file) {
        
        // Process the file (save it, perform operations, etc.)
           
        // For simplicity, this example just prints the file name
               
        String      fileName = file.getOriginalFilename();         System.out.println(
        "Received file: "
        + fileName);        
        // Return a response indicating success
        
        return fileName;
    }**
    
    
}

`if we run the upload call in the postman"http://localhost:6061/awsarTest/auth/upload": we're getting error.

"Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided"

1 Answer 1

1

In your uploaddocument method, the MultipartFile should be annotated with @RequestPart, not @RequestParam (although it's not a problem if you leave it as @RequestParam)

    public String uplaoddocument(@RequestPart("file") MultipartFile file)

Another thing to check would be in your DispatcherServletInitilizer class (or web.xml). You must provide additional configuration to allow the Dispatcher Servlet to handle MultipartFiles

/**
 * Additional configuration for the Dispatcher Servlet
 * @param registration
 */
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    // Set Multipart support to temporarily store uploaded files in the below path
    // Saves uploaded files under /your/upload/location, sets the max file size to 5242880B, the max size of the multipart request to <maxRequestSize>,
    // and max file size that can be uploaded without being written to disk (<FileSizeThreshold> bytes)
    registration.setMultipartConfig( new MultipartConfigElement("/your/upload/location", 5242880, <maxRequestSize>, <FileSizeThreshold>) );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.