Java後臺伺服器接收IOS客戶端上傳的圖片
阿新 • • 發佈:2019-01-10
// 定義變數儲存圖片地址 String imagePath=""; // 接收圖片資料 (base64) String image=httpRequest.getParameter("image"); try{ // 將base64 轉 位元組陣列 Base64 base=new Base64(); byte[]decode=base.decode(image); // 圖片輸出路徑 imagePath=commodityFilePath+"/"+System.currentTimeMillis()+".png"; // 定義圖片輸入流 InputStream fin=new ByteArrayInputStream(decode); // 定義圖片輸出流 FileOutputStream fout=new FileOutputStream(imagePath); // 寫檔案 byte[]b=new byte[1024]; int length=0; while((length=fin.read(b))>0){ fout.write(b,0,length); } // 關閉資料流 fin.close(); fout.close(); }catch(Exception e){ e.printStackTrace(); } IOS端程式碼連結:http://blog.csdn.net/z18694516069/article/details/44806867