java實(shí)現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時(shí)素材
本文實(shí)例為大家分享了java實(shí)現(xiàn)上傳網(wǎng)絡(luò)圖片到微信臨時(shí)素材的具體代碼,供大家參考,具體內(nèi)容如下
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)絡(luò)圖片上傳到微信服務(wù)器
*
* @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)絡(luò)圖片
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方式提交表單,默認(rèn)get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用緩存
// 設(shè)置請(qǐng)求頭信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 設(shè)置邊界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// 請(qǐng)求正文信息
// 第一部分:
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();
// 結(jié)尾部分
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輸入流來(lái)讀取URL的響應(yīng)
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請(qǐng)求出現(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");
}
}
來(lái)源:UploadPicToWx.java
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入操作
這篇文章主要介紹了java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Java中?equals?重寫(xiě)時(shí)為什么一定也要重寫(xiě)?hashCode
這篇文章主要介紹了Java中?equals?重寫(xiě)時(shí)為什么一定也要重寫(xiě)?hashCode,equals?方法和?hashCode?方法是?Object?類(lèi)中的兩個(gè)基礎(chǔ)方法,它們共同協(xié)作來(lái)判斷兩個(gè)對(duì)象是否相等,所以之間到底有什么聯(lián)系呢,接下來(lái)和小編一起進(jìn)入文章學(xué)習(xí)該內(nèi)容吧2022-05-05
mybatis設(shè)置sql執(zhí)行時(shí)間超時(shí)時(shí)間的方法
本文主要介紹了mybatis設(shè)置sql執(zhí)行時(shí)間超時(shí)時(shí)間的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
重寫(xiě)equals的同時(shí)為何要重寫(xiě)hashCode?
這篇文章主要給大家介紹了關(guān)于重寫(xiě)equals的同時(shí)為何要重寫(xiě)hashCode的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

