這篇文章主要為大家展示了java map如何轉(zhuǎn)Multipart/form-data類型,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。
創(chuàng)新互聯(lián)建站制作網(wǎng)站網(wǎng)頁(yè)找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為千余家服務(wù),創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷推廣服務(wù)!
我就廢話不多說了,大家還是直接看代碼吧!
public static String mapToTxt(MapfieldMap, Map 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 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 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 MapTest(HttpServletRequest request,HttpServletRequest response) throws Exception { Map returnMap = new HashMap (); 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 resultMapsReturn = new HashMap<>(); String imagePath="C:\\Users\\win\\Desktop\\1.jpg"http://把取出來的二進(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(); }
以上就是關(guān)于java map如何轉(zhuǎn)Multipart/form-data類型的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。