SpringBoot+SpringCloud 搭建环境,使用webuploade插件跨域上传文件遇到问题

开发环境:SpringBoot 2.0+SpringCloud +JDK1.8

服务器环境:Hadoop+Hbase+Hdfs

关于 百度的webupload插件不做详细解释,网上教程也很多,这里附个地址http://fex.baidu.com/webuploader/

开始:项目为SpringMVC,使用webUploader上传文件正常使用,后来项目拆分为分布式架构,文件上传作为单独的服务器

先抛出遇到的问题:

1、访问URL提示没有 No 'Access-Control-Allow-Origin' header is present on the requested resource


2、SpringBoot 文件上传做限制小文件可以上传,大文件上传不了


3、文件上传成功,合并分块失败,文件取不到,但是request中却有分块信息

问题解决:

1、是因为上传做了跨域限制

解决办法 :方式多种,网上很多但是不好用,只有一种可以生效

  private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
//      corsConfiguration.addExposedHeader(HttpHeaderConStant.X_TOTAL_COUNT);
        return corsConfiguration;
    }

    /**
     * 跨域过滤器
     *
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }



2、springboot文件上传大小做了限制

SpringBoot做文件上传时出现了The field file exceeds its maximum permitted size of 1048576 bytes.错误,显示文件的大小超出了允许的范围。查看了官方文档,原来Spring Boot工程嵌入的tomcat限制了请求的文件大小,这一点在Spring Boot的官方文档中有说明,原文如下

65.5 Handling Multipart File Uploads
Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1Mb per file and a maximum of 10Mb of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the multipart.maxFileSize property to -1.The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.

文档说明表示,每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。要更改这个默认值需要在配置文件(如application.properties)中加入两个配置

需要设置以下两个参数

multipart.maxFileSize
multipart.maxRequestSize

Spring Boot 1.3.x或者之前

multipart.maxFileSize=100Mb
multipart.maxRequestSize=1000Mb

Spring Boot 1.4.x或者之后

spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=1000Mb

 

Spring Boot 2.0 

spring.servlet.multipart.max-file-size = 10Mb
spring.servlet.multipart.max-request-size=100Mb

 

 

很多人设置了multipart.maxFileSize但是不起作用,是因为1.4版本以上的配置改了,详见官方文档:spring boot 1.4



3、原因是:spring-boot自带的org.springframework.web.multipart.MultipartFile

和Multipart产生冲突,如果同时使用了MultipartResolver 和ServletFileUpload,就会在iter.hasNext()返回false.然后整个循环就跳出去了。整个问题产生的原因是Spring框架先调用了MultipartResolver 来处理http multi-part的请求。这里http multipart的请求已经消耗掉。后面又交给ServletFileUpload ,那么ServletFileUpload 就获取不到相应的multi-part请求。因此将multipartResolve配置去除,问题就解决了。


解决办法:

配置文件中 加入 

spring.servlet.multipart.enabled=false

禁用自带MultipartResolver 

参考文档

https://blog.csdn.net/happy_cheng/article/details/54178392








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值