开发排除jar冲突

<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <!--先排除自带的zookeeper-->
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>

SpringBoot文件上传

指定上传文件大小

spring.http.multipart.maxFileSize=200MB
spring.http.multipart.maxRequestSize=200MB

spring.servlet.multipart.max-request-size = 200MB
spring.servlet.multipart.max-file-size = 200MB

Html表单

<form action="fileUploadController" method="post" enctype="multipart/form-data">
		上传文件:<input type="file" name="filename"/><br/>
		<input type="submit"/>
	</form>

Controller

	@RequestMapping("/fileUploadController")
	public String fileUpload(MultipartFile filename) throws Exception{
		System.out.println(filename.getOriginalFilename());
		filename.transferTo(new File("e:/"+filename.getOriginalFilename()));
		return "ok";
	}

跨域

  • SpringBoot Version 2.4.5

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

///跨域访问配置
@Configuration
public class CorsConfig {
  private CorsConfiguration buildConfig() {
      CorsConfiguration corsConfiguration = new CorsConfiguration();
      corsConfiguration.setAllowCredentials(true);  //sessionid 多次访问一致

      // 允许访问的客户端域名
      List<String> allowedOriginPatterns = new ArrayList<>();
      allowedOriginPatterns.add("*");
      corsConfiguration.setAllowedOriginPatterns(allowedOriginPatterns);
//      corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
      corsConfiguration.addAllowedHeader("*"); // 允许任何头
      corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)
      return corsConfiguration;
  }

  @Bean
  public CorsFilter corsFilter() {
      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置
      return new CorsFilter(source);
  }