java map轉(zhuǎn)Multipart/form-data類型body實例
更新時間:2020年05月05日 19:34:53 作者:qq_33770498
這篇文章主要介紹了java map轉(zhuǎn)Multipart/form-data類型body實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,大家還是直接看代碼吧!
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();
}
補充知識: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中的二進制字段
MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");//file是form-data中二進制字段對應(yīng)的name
System.out.println(multipartFile.getSize());
Map<String, Object> resultMapsReturn = new HashMap<>();
String imagePath="C:\\Users\\win\\Desktop\\1.jpg"http://把取出來的二進制保存圖片到本地
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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
Java常用正則表達式驗證工具類RegexUtils.java
相信大家對正則表達式一定都有所了解和研究,這篇文章主要為大家分享了Java 表單注冊常用正則表達式驗證工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
解決RestTemplate第一次請求響應(yīng)速度較慢的問題
這篇文章主要介紹了解決RestTemplate第一次請求響應(yīng)速度較慢的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
MyBatis動態(tài)SQL中的trim標(biāo)簽的使用方法
這篇文章主要介紹了MyBatis動態(tài)SQL中的trim標(biāo)簽的使用方法,需要的朋友可以參考下2017-05-05
SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏
這篇文章給大家介紹了三步實現(xiàn)SpringBoot全局日志記錄,整合Echarts實現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2024-03-03

