首页 > 解决方案 > java.lang.IllegalStateException 用于在 Spring mvc 中从 jsp 上传文件的问题

问题描述

我有这个问题:我正在尝试从一个简单的 jsp 上传文件,我在许多其他网站上看到过类似的代码,但我有这个错误:

“无法将 'java.lang.String' 类型的值转换为所需的类型 'org.springframework.web.multipart.commons.CommonsMultipartFile';嵌套异常是 java.lang.IllegalStateException:无法转换 'java.lang 类型的值。字符串'到所需类型'org.springframework.web.multipart.commons.CommonsMultipartFile':找不到匹配的编辑器或转换策略“

这是我的jsp:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="ISO-8859-1">
      <title>Insert title here</title>
   </head>
   <body>
      <form action="add" enctype="multipart/form-data">
         <input type=file name="file" size="50">
         <input type="submit" value=" invio ">
      </form>
   </body>
</html>

这是我的控制器:

@RequestMapping("add")
public ModelAndView upload(@RequestParam CommonsMultipartFile file, HttpSession session) {
    String path = session.getServletContext().getRealPath("/");
    String fileName = file.getOriginalFilename();
    System.out.println(path + "   " + fileName);
    try {
        byte barr[] = file.getBytes();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(path + "/" + fileName));
        Object o = stream;
        stream.write(barr);
        stream.flush();
        stream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ModelAndView("success");
}

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>LoginProvaMVC</groupId>
   <artifactId>LoginProvaMVC</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>LoginProvaMVC</name>
   <dependencies>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>5.2.4.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>5.2.4.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
      <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
      <dependency>
         <groupId>commons-io</groupId>
         <artifactId>commons-io</artifactId>
         <version>1.2</version>
      </dependency>
      <dependency>
         <groupId>commons-fileupload</groupId>
         <artifactId>commons-fileupload</artifactId>
         <version>1.3.1</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>5.2.4.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>1.2</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.2.6.RELEASE</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.46</version>
      </dependency>
   </dependencies>
</project>

太感谢了

标签: javaspringspring-mvc

解决方案


method="post"

不见了。

<form:form action="student" method="post" role="form"
                        commandName="student" enctype="multipart/form-data">

应该是这样的。


推荐阅读