文章最后将postman表单提交文件使用简介

需要使用的包:可能还有其他的根据自己的框架不同选择添加

<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.2.1</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>1.3.2</version>
</dependency>

这里就不写前端界面,节约不必要的篇幅.建议使用postman进行测试

常见两种方式:也是出现不明问题最多的方式:

1.使用MultipartFile接收文件

@RequestMapping("/uploadFile")
	public @ResponseBody Object uploadFile(@RequestParam("fileUpload")MultipartFile multipartFile) throws IOException {
		String originalFilename = multipartFile.getOriginalFilename();//文件原始名称
		String name = multipartFile.getName();//文件的表单名称
		InputStream is = multipartFile.getInputStream();
		long l = System.currentTimeMillis();
		FileOutputStream os = new FileOutputStream(new File("./"+l + originalFilename));
		byte[] buff = new byte[1024];
		int i = 0;
		while((i = is.read(buff))!=-1) {
			os.write(buff,0,i);
		}
		os.flush();
		os.close();
		is.close();
		HashMap<String, String> hashMap = new HashMap<String,String>();
		String put = hashMap.put("1", "2");
		System.out.println(put);
		return hashMap;
	}

该方法需要注意的有两点:

1.@RequestParam("")中字符就是表单input的name属性

2.如果上传多个文件就使用MultipartFile[] 来接收文件,同时多个input file标签必须使用同一个name

2.使用MultipartHttpServletRequest完成文件接收工作

@RequestMapping("/uploadFileM")
	public @ResponseBody Object uploadFile2(MultipartHttpServletRequest request) throws IOException {
		List<MultipartFile> list = request.getMultiFileMap().get("fileUpload");
		MultipartFile multipartFile = list.get(0);
		String originalFilename = multipartFile.getOriginalFilename();//文件原始名称
		String name = multipartFile.getName();//文件的表单名称
		InputStream is = multipartFile.getInputStream();
		long l = System.currentTimeMillis();
		FileOutputStream os = new FileOutputStream(new File("./"+l + originalFilename));
		byte[] buff = new byte[1024];
		int i = 0;
		while((i = is.read(buff))!=-1) {
			os.write(buff,0,i);
		}
		os.flush();
		os.close();
		is.close();
		HashMap<String, String> hashMap = new HashMap<String,String>();
		String put = hashMap.put("1", "2");
		System.out.println(put);
		return hashMap;
	}

注意:

List<MultipartFile> list = request.getMultiFileMap().get("fileUpload");

该处的key其实也是input 表单的name属性.所以返回的是list

3.使用HttpServletRequest方式进行接收请求,进行强制转换:这种方式可以的话就很幸福了

@RequestMapping("/uploadFileM")
	public @ResponseBody Object uploadFile2(MultipartHttpServletRequest request) throws IOException {
		List<MultipartFile> list = request.getMultiFileMap().get("fileUpload");
		MultipartFile multipartFile = list.get(0);
		String originalFilename = multipartFile.getOriginalFilename();//文件原始名称
		String name = multipartFile.getName();//文件的表单名称
		InputStream is = multipartFile.getInputStream();
		long l = System.currentTimeMillis();
		FileOutputStream os = new FileOutputStream(new File("./"+l + originalFilename));
		byte[] buff = new byte[1024];
		int i = 0;
		while((i = is.read(buff))!=-1) {
			os.write(buff,0,i);
		}
		os.flush();
		os.close();
		is.close();
		HashMap<String, String> hashMap = new HashMap<String,String>();
		String put = hashMap.put("1", "2");
		System.out.println(put);
		return hashMap;
	}

为什么要用这么多方式,我代码只能写一种呀?

由于框架复杂性,很多会产生冲突,想MultipartFile无法接收到参数,有些框架也不能接受到MultipartHttpServletRequest参数,往往接受到的参数变现为空

对于上方法网上给去最多的方案就是在启动类中添加:话说是spring-boot和MultipartFile冲突

@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
	CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
	return multipartResolver;
}

然而在有些框架下这些依然没有什么用,我们就应该使用Common包自己来写下载逻辑

@RequestMapping("/uploadFileR")
	public @ResponseBody Object uploadFile2(HttpServletRequest request) throws IOException {
		if(!(ServletFileUpload.isMultipartContent(request))) {
			return null;
		}
		
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
//		upload.setFileSizeMax(1024*1024);
//		upload.setFileItemFactory(factory);
		List<FileItem> list = upload.parseRequest(request);
		
		for (FileItem fileItem : list) {
			String originalFilename = fileItem.getFieldName();//文件原始名称
			String name = fileItem.getName();//文件的表单名称
			InputStream is = fileItem.getInputStream();
			long l = System.currentTimeMillis();
			FileOutputStream os = new FileOutputStream(new File("./"+l + originalFilename));
			byte[] buff = new byte[1024];
			int i = 0;
			while((i = is.read(buff))!=-1) {
				os.write(buff,0,i);
			}
			os.flush();
			os.close();
			is.close();
		}
		
		HashMap<String, String> hashMap = new HashMap<String,String>();
		String put = hashMap.put("1", "2");
		System.out.println(put);
		return hashMap;
	}

该方式和框架没有关系,也不依赖与你的input的name属性名称,基本可以避免所有的框架冲突

注意:

我个人遇到的一个问题是使用postman测试时候不能填写Content-Tpye的属性值

由于我想做一个异步线程执行,结果传进去的request参数header头部丢失导致问题

postman表单提交文件使用简介

1.下载或安装客户端(客户端必须注册才能使用),不懂请自信百度,教程很多

2.填写url

spring boot Thymeleaf layui 表单提交 springboot提交post表单_System

3.编辑头部

spring boot Thymeleaf layui 表单提交 springboot提交post表单_multipartfile_02

4.编辑请求体

spring boot Thymeleaf layui 表单提交 springboot提交post表单_System_03