【问题标题】:Spring upload file problemsspring上传文件问题
【发布时间】:2013-04-08 21:35:18
【问题描述】:

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

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

1 - 我有 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 类

@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 - 处理分段上传的控制器

@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

...<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>...

当我提交表单时,我得到了一个例外

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

我不知道为什么。当我删除 @RequestParam 注释时,我调用了我的方法,但文件参数 = null。 我的问题是什么?

【问题讨论】:

    标签: java spring model-view-controller multipart


    【解决方案1】:

    您还需要为您的 web 应用配置 MultipartFilter。根据其 Javadoc,它使用 MultipartResolver 解析多部分请求(但您已经配置了那个)。您需要将其映射到处理文件上传的控制器的(部分)请求路径。

    首先,将MultipartFilter 添加到您的 web.xml:

    <filter>
        <filter-name>multipartFilter</filter-name>
        <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
    </filter>
    

    接下来,将过滤器映射到需要接受文件上传的(部分)URL:

    <filter-mapping>
        <filter-name>multipartFilter</filter-name>
        <url-pattern>/admin/library/upload_file</url-pattern>
    </filter-mapping>
    

    【讨论】:

    • 我不确定如何配置它。我需要更改我的 web.xml 吗?或者我必须创建自己的过滤器扩展 MultipartFilter 并将其映射到 /admin/library/upload_file??
    • 遗憾的是它没有帮助。我添加了这两个部分,但仍然得到 null 作为参数。
    • 默认情况下,此过滤器将查找一个名为“filterMultipartResolver”的 Spring bean,它充当 MultipartResolver。您可以使用 web.xml 中的“multipartResolverBeanName”过滤器 init-param 指定另一个 bean 名称。您能否更改“multipartResolver”bean 的名称,或者修复过滤器的配置,看看是否可行?
    【解决方案2】:

    我可以用

    @Override
        protected void customizeRegistration(ServletRegistration.Dynamic registration) {
    
            MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/",100000, 200000, 50000);
    
            registration.setMultipartConfig(multipartConfigElement);
    
        }
    

    【讨论】:

      【解决方案3】:

      我通过将以下内容添加到我的 spring 配置文件来解决此问题:

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

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

      【讨论】:

        【解决方案4】:

        @user64141 是对的,但如果使用 Java 配置而不是 xml,请尝试

        @Bean
        public MultipartResolver multipartResolver() {
            return new CommonsMultipartResolver();
        }
        

        【讨论】:

          猜你喜欢
          • 2011-07-22
          • 1970-01-01
          • 1970-01-01
          • 2020-04-19
          • 1970-01-01
          • 2020-11-02
          • 2010-10-17
          相关资源
          最近更新 更多