java map轉(zhuǎn)Multipart/form-data類型body實(shí)例
我就廢話不多說了,大家還是直接看代碼吧!
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();}
補(bǔ)充知識: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實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA導(dǎo)入jar包的方法2. 基于Java實(shí)現(xiàn)記事本功能3. docker鏡像完全卸載的操作步驟4. idea刪除項(xiàng)目的操作方法5. IntelliJ IDEA配置Tomcat服務(wù)器的方法6. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法7. idea打開多個(gè)窗口的操作方法8. 使用Maven 搭建 Spring MVC 本地部署Tomcat的詳細(xì)教程9. idea導(dǎo)入maven項(xiàng)目的方法10. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法

網(wǎng)公網(wǎng)安備