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 = "\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<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:\\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(); }
以上這篇java map轉(zhuǎn)Multipart/form-data類型body實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08Java常用正則表達(dá)式驗(yàn)證工具類RegexUtils.java
相信大家對(duì)正則表達(dá)式一定都有所了解和研究,這篇文章主要為大家分享了Java 表單注冊常用正則表達(dá)式驗(yàn)證工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11解決RestTemplate第一次請(qǐng)求響應(yīng)速度較慢的問題
這篇文章主要介紹了解決RestTemplate第一次請(qǐng)求響應(yīng)速度較慢的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java實(shí)現(xiàn)對(duì)象轉(zhuǎn)CSV格式
CSV是一種逗號(hào)分隔值格式的文件,一般用來存儲(chǔ)數(shù)據(jù)的純文本格式文件。Java對(duì)象轉(zhuǎn)CSV,有現(xiàn)成的工具包,commons-lang3 的ReflectionToStringBuilder 就可以簡單的解決的對(duì)象轉(zhuǎn)CSV,快跟隨小編一起學(xué)習(xí)一下吧2022-06-06MyBatis動(dòng)態(tài)SQL中的trim標(biāo)簽的使用方法
這篇文章主要介紹了MyBatis動(dòng)態(tài)SQL中的trim標(biāo)簽的使用方法,需要的朋友可以參考下2017-05-05SpringBoot整合Echarts實(shí)現(xiàn)數(shù)據(jù)大屏
這篇文章給大家介紹了三步實(shí)現(xiàn)SpringBoot全局日志記錄,整合Echarts實(shí)現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03