欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-08-23 09:26:43|栏目:JAVA代码|点击:

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

public static String mapToTxt(Map<String,String> fieldMap, Map<String,File> fileMap,String fileName) throws Exception{
		Random random = new Random();
		int j;
		String getLine = "\r\n";
		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<String,String> 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<String,File> 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<String, Object> Test(HttpServletRequest request,HttpServletRequest response) throws Exception {
     Map<String, String> returnMap = new HashMap<String, String>();
    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<String, Object> 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 nio中的select和channel

栏    目:JAVA代码

下一篇:Java经典设计模式之适配器模式原理与用法详解

本文标题:java map转Multipart/form-data类型body实例

本文地址:http://www.codeinn.net/misctech/169326.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有