java map轉(zhuǎn)Multipart/form-data類(lèi)型body實(shí)例
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧!
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ǔ)充知識(shí):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)制字段對(duì)應(yīng)的name System.out.println(multipartFile.getSize()); Map<String, Object> resultMapsReturn = new HashMap<>(); String imagePath='C:UserswinDesktop1.jpg'//把取出來(lái)的二進(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類(lèi)型body實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaScript實(shí)現(xiàn)留言板實(shí)戰(zhàn)案例2. vscode運(yùn)行php報(bào)錯(cuò)php?not?found解決辦法3. 使用Blazor框架實(shí)現(xiàn)在前端瀏覽器中導(dǎo)入和導(dǎo)出Excel4. Python基于requests庫(kù)爬取網(wǎng)站信息5. ASP基礎(chǔ)知識(shí)Command對(duì)象講解6. 資深程序員:給Python軟件開(kāi)發(fā)測(cè)試的25個(gè)忠告!7. 如何從Python的cmd中獲得.py文件參數(shù)8. 如何在python中執(zhí)行另一個(gè)py文件9. Python-openpyxl表格讀取寫(xiě)入的案例詳解10. python中文本字符處理的簡(jiǎn)單方法記錄

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