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

作者:qq_33770498 时间:2023-04-19 13:16:18 

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


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();
   }

来源:https://blog.csdn.net/qq_33770498/article/details/80525182

标签:java,map,Multipart,form-data
0
投稿

猜你喜欢

  • Android自定义顶部导航栏控件实例代码

    2022-02-11 13:43:16
  • Spring框架通过工厂创建Bean的三种方式实现

    2022-11-23 11:29:54
  • java实现把对象数组通过excel方式导出的功能

    2022-07-01 13:20:09
  • java使用集合实现通讯录功能

    2023-01-30 21:27:15
  • C#中的位操作小结

    2023-08-07 07:01:29
  • java使用stream判断两个list元素的属性并输出方式

    2023-02-06 14:15:18
  • Scala数据库连接池的简单实现

    2023-07-14 14:19:37
  • Java面向对象的封装你了解吗

    2023-11-06 16:32:38
  • 使用Spring自定义实现IOC和依赖注入(注解方式)

    2023-09-16 04:42:35
  • java编程之基于SpringBoot框架实现扫码登录

    2023-02-14 02:39:28
  • Android实现横竖屏切换的实例代码

    2022-06-19 22:11:14
  • Java Spring Dubbo三种SPI机制的区别

    2022-05-04 00:29:51
  • java根据List内对象的属性排序方法

    2022-10-25 10:05:44
  • 基于Spring概念模型:PathMatcher 路径匹配器

    2022-08-20 12:52:38
  • Unity中的静态批处理和动态批处理操作

    2022-09-29 20:12:08
  • Maven中央仓库发布的实现方法

    2023-12-09 07:15:06
  • 通过自定制LogManager实现程序完全自定义的logger

    2022-05-11 06:33:15
  • Java IO流和文件操作实现过程解析

    2022-03-10 02:08:13
  • Mybatis的mapper.xml中if标签test判断的用法说明

    2023-12-23 23:04:16
  • Java语言中&&与& ||与|的区别是什么

    2022-06-10 17:44:09
  • asp之家 软件编程 m.aspxhome.com