我使用 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>
Controller :
@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。
有人可以给我一些帮助吗?
非常感谢!
最佳答案
首先尝试这个基本示例
<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;
}
关于java - Spring 3.0处理文件上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14190128/