如何在MultipartResolver中设置MaxUploadSize(SpringBoot Gradle项目)

时间:2016-06-04 05:12:51

标签: spring-mvc file-upload gradle spring-boot multipartform-data

我正在使用KB大小为GB大小的文件上传。在MvcConfig.java中,设置时,

@Bean
public CommonsMultipartResolver commonsMultipartResolver() {
    final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    commonsMultipartResolver.setMaxUploadSize(-1);
    return commonsMultipartResolver;
}

接受任何大小的文件,我可以上传大小从MB到GB的文件,但它不会接受KB大小的小文件。请建议一种接受任何大小文件的方法。

在MvcConfig中:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

  private static final int MEGABYTE_IN_BYTES = 0;


  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
     registry.addViewController("/index").setViewName("index");
     registry.addViewController("/").setViewName("index");
     registry.addViewController("/login").setViewName("login"); 
  }

  @Bean
  public CommonsMultipartResolver commonsMultipartResolver() {
     final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
     commonsMultipartResolver.setMaxUploadSize(-1);
     return commonsMultipartResolver;
  }

  /*@Bean
  public FilterRegistrationBean multipartFilterRegistrationBean() {
     final MultipartFilter multipartFilter = new MultipartFilter();
     final FilterRegistrationBean filterRegistrationBean = new   FilterRegistrationBean(multipartFilter);
     filterRegistrationBean.addInitParameter("multipartResolverBeanName", "commonsMultipartResolver");
     return filterRegistrationBean;
  }

  @Bean(name = "multipartResolver")
  public StandardServletMultipartResolver resolver() {
     return new StandardServletMultipartResolver();
  }*/
}

在控制器类中:

@RequestMapping(value = "/uploadFile.htm", method = RequestMethod.POST)
public @ResponseBody
ModelAndView handleFile1Upload(@RequestParam("userName") String name,
        Model Model,
        HttpServletResponse response,
        @Validated MultipartFile file
        ,@RequestParam("delimiter") String delimiter
        ,@RequestParam("format") String format
        ,@RequestParam("headerPresent") String headerPresent
        final RedirectAttributes redirectAttributes) {


    System.out.println("working...");
    System.out.println("selected file is::::"+file);

        }catch(Exception e){
            e.printStackTrace();
            status = "fail";
        }finally{
            if (!status.equals("fail"));
        }

    return new ModelAndView("redirect:/home");
}

先谢谢你。

0 个答案:

没有答案