404将文件上传到Spring J2EE时

时间:2013-06-12 05:42:04

标签: java spring java-ee file-upload

我目前的代码:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, headers = "Accept=*")
public void uploadFile(@RequestParam(value = "file") MultipartFile multipartFile, 
                    Model model, HttpServletRequest request, HttpServletResponse response) {

    String fileName = multipartFile.getOriginalFilename();
    System.out.println(fileName);
}

问题是当我要求文件存在时(在value =“file”之后没有“required = false”)然后它找不到我的请求的合适路径(404)。

我已经在浏览器中检查过有一个文件被发送到服务器,名称=“file”:(随后从Chrome浏览器复制粘贴)

------WebKitFormBoundaryS7qP6QevHhFOyAZN
Content-Disposition: form-data; name="file"; filename="testfile"
Content-Type: application/octet-stream
------WebKitFormBoundaryS7qP6QevHhFOyAZN--

我真的可以在这里使用提示,任何人都可以帮助我吗? 提前致谢

1 个答案:

答案 0 :(得分:0)

在映射中缺少以下内容:
servlet context.xml:

 <beans:bean id="multipartResolver"
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

根context.xml中:

<bean id="multipartResolver"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <!--25 mb max--> 
    <property name="maxUploadSize" value="26214400"/>
</bean>

和最后在pom.xml中(因为我运行maven项目,否则你需要罐子)

<!-- Fileupload dependencies --> 
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version> <!-- makesure correct version here -->
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>