java實現(xiàn)上傳網(wǎng)絡圖片到微信臨時素材
更新時間:2018年07月20日 09:56:16 作者:修煉到救贖
這篇文章主要為大家詳細介紹了java實現(xiàn)上傳網(wǎng)絡圖片到微信臨時素材,網(wǎng)絡圖片上傳到微信服務器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)上傳網(wǎng)絡圖片到微信臨時素材的具體代碼,供大家參考,具體內容如下
package org.afuos.playcontrol.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * Created by Mohon on 2017/8/10. */ public class UploadPicToWx { /** * 網(wǎng)絡圖片上傳到微信服務器 * * @param urlPath 圖片路徑 * @return JSONObject * @throws Exception */ public String getMediaIdFromUrl(String urlPath, String fileType) throws Exception { String result = null; String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token= Access_token &type=" + fileType + ""; String fileName = urlPath.substring(urlPath.lastIndexOf("/") + 1); // 獲取網(wǎng)絡圖片 URL mediaUrl = new URL(urlPath); HttpURLConnection meidaConn = (HttpURLConnection) mediaUrl.openConnection(); meidaConn.setDoOutput(true); meidaConn.setRequestMethod("GET"); /** * 第一部分 */ URL urlObj = new URL(url); HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); con.setRequestMethod("POST"); // 以Post方式提交表單,默認get方式 con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); // post方式不能使用緩存 // 設置請求頭信息 con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Charset", "UTF-8"); // 設置邊界 String BOUNDARY = "----------" + System.currentTimeMillis(); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); // 請求正文信息 // 第一部分: StringBuilder sb = new StringBuilder(); sb.append("--"); // 必須多兩道線 sb.append(BOUNDARY); sb.append("\r\n"); sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + fileName + "\"\r\n"); sb.append("Content-Type:application/octet-stream\r\n\r\n"); byte[] head = sb.toString().getBytes("utf-8"); // 獲得輸出流 OutputStream out = new DataOutputStream(con.getOutputStream()); // 輸出表頭 out.write(head); // 文件正文部分 // 把文件已流文件的方式 推入到url中 DataInputStream in = new DataInputStream(meidaConn.getInputStream()); int bytes = 0; byte[] bufferOut = new byte[1024]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } in.close(); // 結尾部分 byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定義最后數(shù)據(jù)分隔線 out.write(foot); out.flush(); out.close(); meidaConn.disconnect(); StringBuffer buffer = new StringBuffer(); BufferedReader reader = null; try { // 定義BufferedReader輸入流來讀取URL的響應 reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } if (result == null) { result = buffer.toString(); } } catch (IOException e) { log.info("發(fā)送POST請求出現(xiàn)異常! {}", e); e.printStackTrace(); throw new IOException("數(shù)據(jù)讀取異常"); } finally { if (reader != null) { reader.close(); } } JSONObject jsonObj = JSON.parseObject(result); log.info("getMediaId jsonObj: {}", jsonObj); return jsonObj.getString("media_id"); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中l(wèi)ist.foreach()和list.stream().foreach()用法詳解
在Java中List是一種常用的集合類,用于存儲一組元素,List提供了多種遍歷元素的方式,包括使用forEach()方法和使用Stream流的forEach()方法,這篇文章主要給大家介紹了關于Java中l(wèi)ist.foreach()和list.stream().foreach()用法的相關資料,需要的朋友可以參考下2024-07-07Mybatis使用typeHandler加密的實現(xiàn)
本文詳細介紹了如何在Mybatis中使用typeHandler對特定字段進行加密處理,涵蓋了從引入依賴、配置Mybatis,到實現(xiàn)typeHandler繼承類和配置mapper層的詳細步驟,為需要在項目中實現(xiàn)字段加密的開發(fā)者提供了參考和借鑒2024-09-09