java怎么post请求同时发送二进制文件和json数据

接口参数列表中的参数加入 HTTP 协议请求报文的 Head 部分中,文件作为 HTTP协议的 Body 部分,以二进制方式传输,Content-Type 为application/octet-stream。

不行吧,你也知道两个都要指定Content-Type才能解析,所以只能传一个,你可以body传文件,其他需要的参数加在url上

我也觉得不能,但是他接口文档里这么写的,-_-||,键值对也没告诉我键是什么

应该是可以的,之前实现过。大概如下:

前端:

<form method="post" action="uploadExcel" enctype="multipart/form-data">
			姓名:<input name="name" />
			性别:<input name="pwd"/>
			<input type="file"  name="file" value="上传"/>
			<input type="submit" value="提交"/>
</form>

 

后端

	@RequestMapping(method=RequestMethod.POST, value="/uploadExcel")
    public void uploadExcel(HttpServletRequest request, HttpServletResponse response) {
	  System.out.println(request);
	  MultipartFile multipartFile = ((MultipartRequest) request).getFile("file");
	  String sourceName = multipartFile.getOriginalFilename(); // 原始文件名
	  String fileType = sourceName.substring(sourceName.lastIndexOf("."));
	  String name=request.getParameter("name");
	  String pwd=request.getParameter("pwd");
	  System.out.println(name);
	  
	  String base = request.getSession().getServletContext().getRealPath("/") + "attachments" + File.separator + "uploadedExcel";
	  File file = new File(base);
	  if(!file.exists()){
	   file.mkdirs();
	  }
	  try{
	   String path=base + File.separator + sourceName;
	   
	   multipartFile.transferTo(new File(path));
	   //service.insert("insertAttachment", attach);
	   //上传成功后读取Excel表格里面的数据
	   System.out.println("路径是"+path);
	   
	  }catch (Exception e) {
	   try {
	    response.getWriter().print("{success:false}");
	   } catch (IOException e1) {
	    e1.printStackTrace();
	   }
	  }
 }

祝你好运。

如果要丢文件,发送json数据,那只能是json格式的字符串,contentType这玩意不能让你又是表单,又是json