Spring Boot: Required part of request 'myFile' is missing

I get "The requested part of the request" myFile "is missing" when I try to save the uploaded file to the DB.

Things I've tried so far:

-check pom.xml and version

-add @Bean for CommonsMultipartResolver

-Listed to use @ModelAttribute instead of @RequestParam in UploadFileController

I couldn't find anything to help me resolve this error. :( Thanks in advance!

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>com.stopCozi</groupId>
<artifactId>stopCozi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>StopCozi</name>
<description>Stop Cozi </description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <start-class>com.stopcozi.StopCoziApplication</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>

</properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- for HTML -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Dependecy for database -->


    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <!-- Security dependency for spring boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

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

</dependencies>

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

      

UploadFile.java

package com.stopcozi.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;
/**
 * 
 * @author Alexandra
 * 
 * We need to write a POJO class to represent an upload file
 * JPA annotations are used to map this model class to the database table.
 */
@Entity
@Table(name = "FILES_UPLOAD")
public class UploadFile {

@Id
@GeneratedValue
@Column(name = "FILE_ID")
private long id;

private String fileName;
private byte[] data;

@ManyToOne
@JoinColumn(name = "user_id")
@JsonBackReference
private User user;


public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

@Column(name = "FILE_NAME")
public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}

@Column(name = "FILE_DATA")
public byte[] getData() {
    return data;
}

public void setData(byte[] data) {
    this.data = data;
}
}

      

UploadFileController.java

package com.stopcozi.controller;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;

import com.stopcozi.domain.UploadFile;
import com.stopcozi.domain.User;
import com.stopcozi.service.UploadFileService;
import com.stopcozi.service.UserService;

/**
* 
* @author Alexandra
* Handles requests for the file upload page.
*/
@Controller
@RequestMapping("/upload")
public class UploadFileController {

 @Autowired
 private UploadFileService uploadService;

 @Autowired
UserService userService;

 @RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
    public String showUploadForm() {
        return "uploadFile";
    }
 @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public String handleFileUpload(@RequestParam("myFile") MultipartFile 
 myFile
         /*  @RequestParam("fileUpload") CommonsMultipartFile[] fileUpload*/, 
 Principal principal) throws Exception {


     if(myFile!=null){
         UploadFile uploadFile = new UploadFile();
         uploadFile.setFileName(myFile.getOriginalFilename());
         uploadFile.setData(myFile.getBytes());

         User user=userService.findByUsername(principal.getName());
         uploadFile.setUser(user);


         uploadService.save(uploadFile);  
     }

        return "uploadFile";
    }  
}

      

uploadFile.html

 <form method="post" th:action="@{/upload/uploadFile}" enctype="multipart/form-data"> 
 <input type="file" name="myFile" size="50" />
 <input type="submit" value="Incarca" />

      

StopCoziApplication.java

@SpringBootApplication
public class StopCoziApplication {

public static void main(String[] args) {
    SpringApplication.run(StopCoziApplication.class, args);
}

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf) {
    return hemf.getSessionFactory();
    }

@Bean(name = "multipartResolver")
public CommonsMultipartResolver getCommonsMultipartResolver() {
    CommonsMultipartResolver multipartResolver = new 
CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(20971520);   // 20MB
    multipartResolver.setMaxInMemorySize(1048576);  // 1MB
    return multipartResolver;
}

@Bean
public MultipartResolver multipartResolver() {
    return new CommonsMultipartResolver();
}

}

      

+3


source to share





All Articles