av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

java map轉Multipart/form-data類型body實例

瀏覽:100日期:2022-09-02 09:48:16

我就廢話不多說了,大家還是直接看代碼吧!

public static String mapToTxt(Map<String,String> fieldMap, Map<String,File> 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<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:UserswinDesktop1.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實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 久久视频在线免费观看 | 91精品国产99久久久久久红楼 | 99中文字幕 | 国产欧美久久久 | 国产欧美一区二区精品性色超碰 | 亚洲欧美日韩国产精品 | 国产强伦人妻毛片 | 成人福利视频在线观看 | 日韩av一区二区在线观看 | 免费视频国产 | 欧洲一级片 | 亚洲精品一区二区三区在线 | 天天射天天操天天干 | 91看片在线 | 国产精品国产成人国产三级 | 8x8ⅹ国产精品一区二区 | 成人毛片100免费观看 | av成人在线播放 | 亚洲天堂色 | 在线观看欧美日韩视频 | 中文在线免费看视频 | 综合99| 久久久久久99精品久久久 | 成人av免费看| 黄免费视频 | 黄色一级在线观看 | 一区二区三区精品 | 久久久蜜桃 | 超碰人人插| 中文字幕理伦片免费看 | 国产二区精品 | 免费国产一区二区 | 日本va欧美va欧美va精品 | 亚洲欧美国产精品 | 中文字幕国产在线 | 国产免费a | 欧美日韩亚洲天堂 | 亚洲激情四射 | 亚洲国产三级 | 国产美女自拍 | 日韩免费精品视频 |