当前位置: 首页 > 知识库问答 >
问题:

在spring boot上载图像不成功

梁巴英
2023-03-14

我有一个表单,我正在用图像提交数据。但当我单击提交按钮时,我得到以下错误:

org.springframework.validation.bindexception:org.springframework.validation.BeanPropertyBindingResult:1错误字段“file data”字段上对象“employee”中的字段错误:拒绝值[org.springframework.web.multipart.support.standardmultipartttpservletrequest$standardmultipartfile@25d17d23];代码[TypeMismatch.Employee.FileData,TypeMismatch.FileData,TypeMismatch.org.SpringFramework.web.MultiPart.CommonsCommonsMultiPartFile,TypeMismatch];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码[employee.fileData,fileData];参数[];default message[fileData]];默认消息[无法将“org.springframework.web.multipart.support.standardmultipartttpservletrequest$standardmultipartfile”类型的属性值转换为属性“file data”的必需类型“org.springframework.web.multipartttpservletrequest$standardmultipartfile”;嵌套异常为java.lang.IllegalStateException:无法将“org.springframework.web.multipart.support.standardmultipartfile”类型的值转换为属性“file data”的

我正在使用spring boot,我做了以下工作:

应用程序.属性

# multipart
multipart.enabled=true
spring.http.multipart.max-file-size=500000KB
spring.http.multipart.max-request-size=500000KB

我的GET和POST方法处理如下:

  @GetMapping("/add-employee")
    public ModelAndView empAdmin(Model model) {
        Employee employee=new Employee();
        model.addAttribute("employee", employee);
        return new ModelAndView("add-new-employee");
    }

    // POST: Save product
    @RequestMapping(value = { "/add-employee" }, method = RequestMethod.POST)
    public String productSave(@ModelAttribute("employee") Employee employee)

    {      
        employeeService.saveEmployee(employee);

        return "redirect:/add-employee";
    }

我的服务类别是:

import com.ashwin.vemployee.model.Employee;
import com.ashwin.vemployee.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository empRepository;

    public Employee saveEmployee(Employee employee){
        byte[] image = employee.getFileData().getBytes();
        if (image != null && image.length > 0) {
            employee.setImage(image);
        }
        if(employee.getiNumber()==null){
            empRepository.save(employee);
        }
        else{
            empRepository.save(employee);
        }
        return  employee;
    }

}

我的add-new-employee.jsp如下所示:

<form:form modelAttribute="employee" method="POST" enctype="multipart/form-data">

    <label>iNumber</label>
    <form:input path="iNumber" id="iNumber" type="text" class="form-control" required="required" />

    <label>Full Name:</label>
    <form:input path="fullName" id="fullName" type="text" class="form-control" required="required" />

    <label>Joined Date</label>
    <form:input path="joinedDate" id="joinedDate" type="text" class="form-control" required="required" />

    <label>Position</label>
    <form:input path="position" id="position" type="text" class="form-control" required="required" />

    <label>Reports To</label>
    <form:input path="reportsTo" id="reportsTo" type="text" class="form-control" required="required" />

    <label>Cubicle No</label>
    <form:input path="cubicleNo" id="cubicleNo" type="text" class="form-control" required="required" />

    <label>Job type</label>
    <form:input path="jobType" id="jobType" type="text" class="form-control" required="required" />

    <td>Upload Image</td>
    <td>
        <form:input type="file" path="fileData" />
    </td>
    <td> </td>

    <input type="submit" value="Submit" />
    <input type="reset" value="Reset" />

</form:form>

为了处理上载文件选项,我添加了 。我的模型类类似于以下employee.java.

    import org.springframework.web.multipart.commons.CommonsMultipartFile;

    import javax.persistence.*;
    import javax.validation.constraints.NotBlank;
    import java.util.Date;

    @Entity
    @Table(name = "employee")
    public class Employee {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;

        @NotBlank
        private String iNumber;

        @NotBlank
        private String fullName;

    //    @NotBlank
        private String joinedDate;

        @NotBlank
        private String position;

        @NotBlank
        private String reportsTo;

        @NotBlank
        private String cubicleNo;

        @NotBlank
        private String jobType;

        @Lob
        @Column(name = "Image", length = Integer.MAX_VALUE, nullable = true)
        private byte[] image;

        // Upload file.
        //@Transient
        private CommonsMultipartFile fileData;

//all default construcotr and getters and setters

我在pom.xml中添加了新的依赖项。

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

我的整个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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ashwin</groupId>
    <artifactId>vemployee</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>vemployee</name>
    <description>Demo project for Spring Boot for offc</description>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- needed for jsp -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.27</version>
        </dependency>

        <!--bootsrap and jquery-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.4.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.webjars/bootstrap-datepicker -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap-datepicker</artifactId>
            <version>1.7.1</version>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

为什么我会出现这个错误?

共有2个答案

慕项明
2023-03-14

在类中,不应该将多部分文件称为commonsmultipartfile,它只是multipartfile

艾雪风
2023-03-14

如果图像数据在那里,你可以尝试在下面获取请求部分。

 // POST: Save product
        @RequestMapping(value = { "/add-employee" }, method = RequestMethod.POST, consumes = {"multipart/form-data"})
        public String productSave(@ModelAttribute("employee") Employee employee,  @RequestPart("file") MultipartFile file)

        {      
            employeeService.saveEmployee(employee);

            return "redirect:/add-employee";
        }

另一个解决方案是Base64编码您的图像并作为字符串发送,然后在服务层解码它。

 类似资料:
  • 的控制台输出是正确的,我相信使用的是正确的。 上载的控制台输出如下: 然而,当我查看firebase存储时,新文件夹和图像不在那里。火药库没有变化。为什么它实际上不储存在仓库里?

  • 我无法上载firebase存储上的配置文件图像。我正在把烤面包片上的错误打印出来。为什么会出现这种情况?我无法解决这个问题,我审查了我的代码很多次。问题可能是什么?我如何调整我的代码以使其正常工作?提前向大家表示感谢。

  • 我正在尝试加载存储在应用程序文件夹上的图像当我打开活动时,我从logcat收到了这个错误: 1-20 01:30:40.125 14331-14331/mx.eusaga。af W/ImageView:无法打开内容:content://mx.eusaga.af.fileprovider/imagenes/1-db384fa8-f28b-4595-abbf-d45d38fd4036.jpgjava.

  • 我需要一些上传图像的帮助。 我有两个不同的申请 在我的专用服务器上运行的Springstart应用程序 Angulal-6 Application-在我的云服务器上运行 我需要从我的spring boot应用程序上传一个图像到Angular-6资产文件夹(资产文件夹类似于spring boot应用程序的资源文件夹) 在application.properties我已经声明上传位置,如profile

  • 你好,请帮我在图像视图中需要上传当前图像 我需要上传电话中的图像捕获`公共类MainActivity extends Activity{Button Button;private static final int CAMERA_REQUEST=1888;private ImageView ImageView; //在main中找到按钮。xml按钮=(按钮)findViewById(R.id.upl

  • 步骤1:登录到应用程序并上传管理配置文件的图像。应用程序上传图像成功,没有被记录在Jmeta。步骤2:启动Jmetm,添加一个线程组。步骤3:添加HTTP(S)测试脚本记录器,转到HTTP采样器设置,并选择类型为Java。步骤3:选择目标控制器为线程组,将Firefox配置为端口 8080,并启动测试脚本记录器上传图像。结果:应用程序无法成功上传图像时,脚本记录的Jmetm。显示的错误是:"jav