栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java map转Multipart/form-data类型body实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java map转Multipart/form-data类型body实例

我就废话不多说了,大家还是直接看代码吧!

public static String mapToTxt(Map fieldMap, Map fileMap,String fileName) throws Exception{
		Random random = new Random();
		int j;
		String getLine = "rn";
		String fileType = "Content-Type: application/octet-stream";
		String doubleBar = "--";
		biaoshi = "----WebKitFormBoundary";
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < 16;i++){
			j = random.nextInt(MULTIPART_CHARS.length-2)+2;
			sb.append(MULTIPART_CHARS[j]);
		}
		biaoshi = biaoshi + sb.toString();
		StringBuffer stringBuffer = new StringBuffer();
 
 
 
		for (Map.Entry entity:fieldMap.entrySet()) {
			String name = "Content-Disposition: form-data; name=""+entity.getKey()+""";
			stringBuffer.append(doubleBar+biaoshi);
			stringBuffer.append(getLine);
			stringBuffer.append(name);
			stringBuffer.append(getLine);
			stringBuffer.append(getLine);
			stringBuffer.append(entity.getValue());
			stringBuffer.append(getLine);
		}
 
		for (Map.Entry entity:fileMap.entrySet()) {
			String name = "Content-Disposition: form-data; name=""+fileName+""; filename=""+entity.getValue().getName()+""";
			stringBuffer.append(doubleBar+biaoshi);
			stringBuffer.append(getLine);
			stringBuffer.append(name);
			stringBuffer.append(getLine);
			stringBuffer.append(fileType);
			stringBuffer.append(getLine);
			stringBuffer.append(getLine);
			File f = entity.getValue();
			FileInputStream fileInputStream = new FileInputStream(f);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			byte by[] = new byte[1024];
			int k = 0;
			while ((k=fileInputStream.read(by))!=-1){
				byteArrayOutputStream.write(by,0,k);
			}
			by = byteArrayOutputStream.toByteArray();
			for(int i = 0; i < by.length; i++){
				stringBuffer.append(by[i]);
			}
			stringBuffer.append(getLine);
		}
		stringBuffer.append(doubleBar+biaoshi+doubleBar);
		return stringBuffer.toString();
	}

补充知识:java 如何取出传参数格式为form-data中的值

 public Map Test(HttpServletRequest request,HttpServletRequest response) throws Exception {
     Map returnMap = new HashMap();
    String a=request.getParameter("a");//取出form-data中a的值
    String b=request.getParameter("b");//取出form-data中a的值
    //取出form-data中的二进制字段
    MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request; 
    MultipartFile multipartFile = multipartRequest.getFile("file");//file是form-data中二进制字段对应的name
    System.out.println(multipartFile.getSize());  
    Map resultMapsReturn = new HashMap<>();
    String imagePath="C:\Users\win\Desktop\1.jpg"//把取出来的二进制保存图片到本地
    if(multipartFile.getSize()<=0){
      resultMapsReturn.put("resultcode", "0");
      resultMapsReturn.put("msg", DisWebConst.ERROR_TITLE);
    }else{
      InputStream is = multipartFile.getInputStream();

      OutputStream out = new FileOutputStream(imagePath);
      IOUtils.copy(is, out);
      is.close();
      out.close();
    }

以上这篇java map转Multipart/form-data类型body实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号