欢迎访问优讯网!
您当前的位置:首页 > 爱编程

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

时间:2020-05-06 10:04:12  来源:优讯网  作者:小卡司  浏览次数:
这篇文章主要介绍了java map转Multipart/form-data类型body实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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中的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 map转Multipart/form-data类型body实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

来顶一下
返回首页
返回首页

原文链接:https://www.jb51.net/article/185961.htm


推荐资讯
如何下载旧版centos iso镜像 如何下载迷你mini版的centos镜像
如何下载旧版centos i
计算机的正确使用姿势 电脑痴如何正确的使用电脑
计算机的正确使用姿势
好用的后台管理的前端框架模版H-ui H-ui框架模版分享
好用的后台管理的前端
微信电脑多开方法 无需辅助电脑版微信双开方法分享
微信电脑多开方法 无
相关文章
栏目更新
栏目热门