Spring上传文件问题 [英] Spring upload file problems

查看:148
本文介绍了Spring上传文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件从浏览器上传到服务器。我使用spring 3.2作为我的web框架。

I need to upload file from browser to server. I use spring 3.2 as my web framework.

所以我这样配置我的应用程序。

So i configured my app like this.

1 - I得到web.xml

1 - I got web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>racoonsoft.chaos.settings</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>admin/library</welcome-file>
    </welcome-file-list>

</web-app>

2 - MainConfig class

2 - MainConfig class

@Configuration
@Import({WebConfig.class })
public class MainConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public static ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor() {
        return new ScheduledAnnotationBeanPostProcessor();
    }

    @Bean
    public static StandardServletMultipartResolver multipartResolver() {
        StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
        return resolver;
    }
}

3 - 用于处理分段上传的控制器

3 - Controller to handle multipart uploads

@Controller
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
        maxFileSize=1024*1024*10,      // 10MB
        maxRequestSize=1024*1024*50)
public class FileUpload
{
    public static final int UPLOAD_RESULT_OK = 100000;
    @Autowired
    BookDao book_dao;

    @RequestMapping(value = "/admin/library/upload_file", method = RequestMethod.POST)
    public String saveFiles(@RequestParam("file-file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            byte[] bytes = file.getBytes();
            return "redirect:caps/total_fail";
        }
        else
        {
            return "redirect:caps/total_fail";
        }
    }
}

4 - 我放置的jsp我的表单提交文件

4 - jsp where i placed my form to submit files

...<form method="post" action="/admin/library/upload_file" enctype="multipart/form-data">
                <input type="text" name="name"/>
                <input type="file" name="file-file"/>
                <input type="submit"/>
            </form>...

当我提交表单时,我遇到了例外情况

When i submit my form i got an Exception

org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present
    org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)

我有不明白为什么。当我删除@RequestParam annotaion时,我调用了我的方法但是file parameter = null。
我的问题是什么?

I got no idea why. When i remove @RequestParam annotaion i got my method invoked but file parameter = null. What is my problem?

推荐答案

我通过在弹簧配置文件中添加以下内容解决了这个问题:

I fixed this problem by adding the following to my spring config file:

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

(我得到的错误是org.springframework.web.bind.MissingServletRequestParameterException:必需的MultipartFile参数'myFile'不存在

(The error I was getting was "org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'myFile' is not present

这篇关于Spring上传文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆