java - 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>
太感谢了
解决方案
method="post"
不见了。
<form:form action="student" method="post" role="form"
commandName="student" enctype="multipart/form-data">
应该是这样的。
推荐阅读
- c - 在 C 中从华氏温度转换为摄氏温度,反之亦然
- javascript - 仅当用户开始在输入字段中输入时,如何从输入值中删除某些警告文本?
- python - 如何将长语音转换为文本。Python 中的 Google 异步语音识别
- c++ - 如何返回可选指针或引用( std::optional )?
- angularjs - 如何处理带有角度双斜杠的网址?
- python - 将带有 numpy 变体的代码替换为带有 pandas 变体的代码时出错
- ffmpeg - 在视频上使用 FFMPEG,而不是创建标签
- docusignapi - '用户 ID xxx 的 IntegratorKey xxxx 无权访问 API 版本 v2' Docusign JWT API 错误
- reactjs - 如何使用 onClick 转到另一个页面?
- android - 如何从 recyclerView 的 onClickListener 的活动中打开片段