SpringBoot下多文件上传详解例子

springBoot下直接上传getFile可能会为null MultipartResolver获取后上传,路径必须明确到磁盘,
springBoot无法识别非磁盘路径。

springboot下需要在Application主线程下禁用掉自带的文件上传

添加 @EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})

eg:如下↓

package com.kero99;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.kero99.mapper")
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {"com.kero99"})
//开启定时任务
@EnableScheduling
//开启异步调用方法
@EnableAsync
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Application.class);
	}
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args); 
	}	
	
}

上传文件Demo

	@RequestMapping(value="/upload")
	@Transactional(propagation=Propagation.SUPPORTS)
	public  boolean uploadZip(
	HttpServletRequest request,HttpServletResponse response){
		MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
		// MultipartHttpServletRequest mh=(MultipartHttpServletRequest) request;
		// CommonsMultipartFile cmf = (CommonsMultipartFile)
		// multipartRequest.getFile("file");
		// 获得文件
		// CommonsMultipartFile cmf=(CommonsMultipartFile) mh.getFile("file");
		Iterator iter = multipartRequest.getFileNames();
		while (iter.hasNext()) {
			MultipartFile file = multipartRequest.getFile(iter.next().toString());			
			if(file.isEmpty()) {
				continue;
			}else {
				String fileName = file.getOriginalFilename();
				// 文件路径,可以替换为自己的文件夹
				String savePath = Commons.SHARE_PATH;
				// String savePath="E:\\AMapUsermark";
				File storeDirectory = new File(savePath);
				if (!storeDirectory.exists()) {
					storeDirectory.mkdirs();
				}
				// 文件路径+文件名
				String path = savePath + File.separator + fileName;
				try {
					// 保存文件
					file.transferTo(new File(path));
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
		return true;
	}

es3 jquery前台





Insert title here



   


 

 

你可能感兴趣的:(Java,程序式界)