java.lang.IllegalStateSpring mvc中从jsp上传文件的问题出现异常



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

"未能将类型"java.lang.String"的值转换为所需类型"org.springframework.web.multipat.commons.CommonsMultipartFile";嵌套异常为java.lang.IllegalStateException:无法将"java.lang.String"类型的值转换为所需的"org.springframework.web.multipat.commons.MultipartFile"类型:找不到匹配的编辑器或转换策略">

这是我的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>

非常感谢

method="post"

缺少。

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

应该是这样的。

最新更新