java 文件空指针,java – Spring 3中的文件上传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

HTML / JSP文件

JSP Page

${Response}

Upload Songs

Song Name :
Artist Name :
Gendre :
description :
Browse File :

表单处理程序类

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;

}

}

控制器

@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 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";

}

}

任何帮助将非常感激.提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值