我使用Spring MVC 3.0进行文件上传, 我已经关注了如何使用spring上传文件的几个在线教程。 但是,我一直无法获取我的文件,在提交表单时它总是为空。
请在下面找到我的代码:
查看:
<form:form action="processXML" modelAttribute="uploadXML" method="post" enctype="multipart/form-data">
<div>
<table>
<tr>
<td>
<input name="uploadXML" type="file"/>
</td>
</tr>
</table>
<input type="submit"/>
</div>
控制器:
@RequestMapping(value="processXML", method = RequestMethod.POST)
public ModelAndView processXML(@ModelAttribute("uploadXML") UploadXML uploadXML, ModelMap model) {
logger.info("Start processing import file.");
ModelAndView modelAndView = new ModelAndView("import");
//modelAndView.addObject("courseId", courseId);
logger.info("Data: " + uploadXML.getFile().getName());
logger.info("Data 2: " + uploadXML.getFile().getContentType());
logger.info("Data 3: " + uploadXML.getFile().getSize());
return modelAndView;
}
UploadXML.java
public class UploadXML {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
我还包括:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
进入我的servlet.xml。
有人能给我一些帮助吗?
非常感谢!
答案 0 :(得分:1)
首先尝试这个基本示例
<form:form action="processXML" method="post" enctype="multipart/form-data">
<div>
<table>
<tr>
<td>
<input name="uploadXMLFile" type="file"/>
</td>
</tr>
</table>
<input type="submit"/>
</div>
</form:form>
@RequestMapping(value="processXML", method = RequestMethod.POST)
public ModelAndView processXML(@RequestParam("uploadXMLFile") CommonsMultipartFile file, ModelMap model) {
logger.info("Start processing import file.");
ModelAndView modelAndView = new ModelAndView("import");
//modelAndView.addObject("courseId", courseId);
logger.info("Data: " + file.getName());
logger.info("Data 3: " + file.getSize());
return modelAndView;
}