use*_*190 8 java file-upload spring-mvc
我正在使用spring MVC 3.我一直在尝试访问上传文件的属性,但我不断收到以下错误消息.我可以访问已发布的表单的其他字段,但我无法访问上传的文件.
nullhandleForm - Failed to convert property value of type 'java.lang.String' to required type
'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'file';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]
for property 'file': no matching editors or conversion strategy found
Run Code Online (Sandbox Code Playgroud)
HTML/JSP文件
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p>${Response}</p>
<h1>Upload Songs</h1>
<table>
<form:form action="" commandName="handleForm">
<tr>
<td>Song Name :</td>
<td><form:input path="songName"/></td>
</tr>
<tr>
<td>Artist Name :</td>
<td><form:input path="artistName"/></td>
</tr>
<tr>
<td>Gendre :</td>
<td><form:input path="gendre"/></td>
</tr>
<tr>
<td>description :</td>
<td><form:textarea path="description"/></td>
</tr>
<tr>
<td>Browse File :</td>
<td><form:input type="file" path="file" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center"><input type="submit" value="submit" /></td>
</tr>
</form:form>
</table>
</body>
Run Code Online (Sandbox Code Playgroud)
表单处理程序类
public class HandleForm {
private String songName;
private String artistName;
private String gendre;
private String description;
private CommonsMultipartFile file;
public CommonsMultipartFile getFile() {
return file;
}
public void setFile(CommonsMultipartFile file) {
this.file = file;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGendre() {
return gendre;
}
public void setGendre(String gendre) {
this.gendre = gendre;
}
public String getSongName() {
return songName;
}
public void setSongName(String songName) {
this.songName = songName;
}
}
Run Code Online (Sandbox Code Playgroud)
控制器
@Controller
public class AdminController{
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public String showAdmin(){
return "admin/index";
}
@RequestMapping(value = "/admin/upload-songs", method = RequestMethod.GET)
public String showContacts(Model model) {
model.addAttribute(new HandleForm());
return "/admin/upload";
}
@RequestMapping(value = "/admin/upload-songs", method = RequestMethod.POST)
public String doForm(@ModelAttribute(value = "handleForm") HandleForm handleForm, BindingResult result, Model model){
if(result.hasErrors()){
String stringList = null;
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
stringList += error.getObjectName() + " - " + error.getDefaultMessage() + "\n";
}
model.addAttribute("Response", stringList);
//model.addAttribute("songname", handleForm.getSongName()); works fine
//model.addAttribute("filename", handleForm.getFile().getOriginalFilename()); throws an error..?
}
return "/admin/upload";
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.提前致谢
dig*_*oel 17
你需要在表单声明中添加enctype吗?
enctype="multipart/form-data"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11521 次 |
| 最近记录: |