0

我正在使用 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;
}

上传XML.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。

谁能给我一些帮助?

非常感谢!

4

1 回答 1

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;
}
于 2013-01-07T05:10:14.857 回答