httpClient 的 MultipartFile 的上传与接收 hutools工具

上传

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
    @GetMapping("sendFlie")
	void send1(HttpServletRequest request) throws IOException {

		File file1 = new File("a1.txt");
		File file2 = new File("a2.txt");
		File file3 = new File("a3.txt");

		//List files = Lists.newArrayList();
		//files.add(file1);
		//files.add(file2);
		//files.add(file3);

		HttpRequest post = HttpRequest.post("localhost:8080/receive");
		post.header("Content-Type", "multipart/form-data"); //设置请求头方式

		Map map = Maps.newHashMap();
		map.put("aaa",123);
		map.put("bbb",45465);
		map.put("file1",file1);
		map.put("file2",file2);
		map.put("file3",file3);

		post.form(map);
		HttpResponse execute = post.execute();
		System.out.println("execute = " + execute.body());
	}

接收

@PostMapping("receive")
	void up(HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {

			//将HttpServletRequest转为MultipartHttpServletRequest

//			 这个resolver不好使
//			MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//			MultipartHttpServletRequest multiRequest = resolver.resolveMultipart(request);

			//这个resolver好使
			StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
			MultipartHttpServletRequest multiRequest = resolver.resolveMultipart(request);
			if (resolver.isMultipart(request)) {
				//解析request的 请求参数取值
				String aaa = request.getParameter("aaa");
				System.out.println("aaa = " + aaa);
				aaa = multiRequest.getParameter("bbb");
				System.out.println("bbb = " + aaa);

				//List files = multiRequest.getFiles("files");
				//System.out.println("files.size() = " + files.size()); // 不建议 看源码其实从getFileMap里面获取的
				//List filesArr = multiRequest.getFiles("filesArr");
				//System.out.println("filesArr.size() = " + filesArr.size()); //不建议 看源码其实从getFileMap里面获取的
				Map fileMap = multiRequest.getFileMap(); // 好使
				//文件的其他操作
				for (String s : fileMap.keySet()) {
					System.out.println(" 文件名称为:= " + s);
					MultipartFile multipartFile = fileMap.get(s);
					String originalFilename = multipartFile.getOriginalFilename();
					File localFile = new File("D://" + originalFilename);
					multipartFile.transferTo(localFile);
				}
			}
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");



			response.getOutputStream().write("OK".getBytes("utf-8"));
		} catch (Exception e) {
			response.getWriter().write(e.getMessage());
		}
	}

控制台输出就不贴了, 可自行验证.

文件可以获取且保存到D盘

借鉴

Springboot解决入参HttpServletRequest获取文件失败问题 – 源码巴士

java文件上传与接收处理_java 接收上传文件_guzhizang的博客-CSDN博客

从request中获取文件_request 获取文件_程序猿00132的博客-CSDN博客

你可能感兴趣的:(windows)