溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java map如何轉Multipart/form-data類型

發(fā)布時間:2020-07-29 09:11:36 來源:億速云 閱讀:698 作者:小豬 欄目:編程語言

這篇文章主要為大家展示了java map如何轉Multipart/form-data類型,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

我就廢話不多說了,大家還是直接看代碼吧!

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中二進制字段對應的name
    System.out.println(multipartFile.getSize());  
    Map<String, Object> resultMapsReturn = new HashMap<>();
    String imagePath="C:\\Users\\win\\Desktop\\1.jpg"//把取出來的二進制保存圖片到本地
    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如何轉Multipart/form-data類型的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI