国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

java map轉(zhuǎn)Multipart/form-data類型body實例

瀏覽:109日期: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 如何取出傳參數(shù)格式為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中的二進(jìn)制字段 MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile('file');//file是form-data中二進(jìn)制字段對應(yīng)的name System.out.println(multipartFile.getSize()); Map<String, Object> resultMapsReturn = new HashMap<>(); String imagePath='C:UserswinDesktop1.jpg'//把取出來的二進(jìn)制保存圖片到本地 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轉(zhuǎn)Multipart/form-data類型body實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 沙河市| 星子县| 临安市| 巩义市| 微博| 安阳县| 富蕴县| 青铜峡市| 巴南区| 大宁县| 蓬莱市| 略阳县| 阿荣旗| 开原市| 婺源县| 广平县| 徐州市| 佛山市| 崇明县| 大余县| 清徐县| 新沂市| 化德县| 通州市| 旅游| 南江县| 河东区| 台南县| 龙州县| 江陵县| 三河市| 瓦房店市| 靖边县| 大庆市| 伊吾县| 三原县| 沽源县| 和硕县| 龙川县| 宜兰县| 禄丰县|