1. 程式人生 > >微信公眾號開發學習(3)_____新增臨時和永久素材

微信公眾號開發學習(3)_____新增臨時和永久素材

完成與微信使用者的互動,但多數訊息需要檔案支援的(圖片、語音等等),微信有很多專門用來儲存檔案的介面

//臨時素材介面路徑
static final String SET_TEMPORARY_MATERIAL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=";
//呼叫介面憑證
String ACCESS_TOKEN = "13_ajKU3NczC5pYaXNWH1Avv-dtiiuqNXC_k3RCEwXgmuutHhQS9JlZZJ-U3HdBXGWAXa1pfXXuk_Zw-ezWv6NoGv_jeRaKTcHxt3ZV5nsAzMCrJ85ASTDhGCUb4yyWvPmWwVImMx7qyxdUCW7KFLBdACAJPF";

HttpURLConnection huct = (HttpURLConnection) new URL(SET_TEMPORARY_MATERIAL + ACCESS_TOKEN+"&type=image").openConnection();
huct.setRequestMethod("POST");//注意,只能以POST方式上傳檔案
huct.setDoOutput(true);//設定可以有返回值

File file = new File("雲煙成雨.jpg");

huct.setRequestProperty("Content-Type", "multipart/form-data; boundary=-------------------146043902153");
OutputStream os = huct.getOutputStream();
StringBuilder strb = new StringBuilder();
//設定頭部分割符
strb.append("\r\n---------------------146043902153\r\n");
strb.append("Content-Disposition:form-data;name=\"media\";");
strb.append("filename=\""+file.getName()+"\";");//圖片名稱
strb.append("Content-Type:\"image\";encoding=utf-8;");//image媒體檔案型別和設定編碼格式
strb.append("filelength:\"" + file.length() + "\"\r\n");//檔案大小
strb.append("application/octet-stream\r\n\r\n");
os.write(strb.toString().getBytes("UTF-8"));

FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[fis.available()];
fis.read(bt);
fis.close();
os.write(bt);
//尾部分割符
os.write("\r\n---------------------146043902153--\r\n\r\n".getBytes());
os.flush();
os.close();

InputStream is = huct.getInputStream();
byte[] by = new byte[is.available()];
is.read(by);
System.out.println(new String(by));//返回資料
//永久素材介面路徑
static final String SET_PERMANENT_MATERIAL = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=";
//呼叫介面憑證
String ACCESS_TOKEN = "13_ajKU3NczC5pYaXNWH1Avv-dtiiuqNXC_k3RCEwXgmuutHhQS9JlZZJ-U3HdBXGWAXa1pfXXuk_Zw-ezWv6NoGv_jeRaKTcHxt3ZV5nsAzMCrJ85ASTDhGCUb4yyWvPmWwVImMx7qyxdUCW7KFLBdACAJPF";

HttpURLConnection huct = (HttpURLConnection) new URL(SET_TEMPORARY_MATERIAL + ACCESS_TOKEN+"&type=video").openConnection();
huct.setRequestMethod("POST");//注意,只能以POST方式上傳檔案
huct.setDoOutput(true);//設定可以有返回值

File file = new File("終級斬殺.mp4");

huct.setRequestProperty("Content-Type", "multipart/form-data; boundary=-------------------146043902153");
OutputStream os = huct.getOutputStream();
StringBuilder strb = new StringBuilder();
//設定頭部分割符
strb.append("\r\n---------------------146043902153\r\n");
strb.append("Content-Disposition:form-data;name=\"media\";");
strb.append("filename=\""+file.getName()+"\";");//圖片名稱
strb.append("Content-Type:\"video\";encoding=utf-8;");//video媒體檔案型別和設定編碼格式
strb.append("filelength:\"" + file.length() + "\"\r\n");//檔案大小
strb.append("application/octet-stream\r\n\r\n");
os.write(strb.toString().getBytes("UTF-8"));

FileInputStream fis = new FileInputStream(file);
byte[] bt = new byte[fis.available()];
fis.read(bt);
fis.close();
os.write(bt);

String perpetual = "";//建立永久視訊素材時,要額外新增表單資料
perpetual += "\r\n---------------------146043902153\r\n";
perpetual += "Content-Disposition:form-data;name=\"description\";\r\n\r\n";
perpetual += ("{\"title\":\"視訊素材的標題\",\"introduction\":\"視訊素材的描述\"}");
os.write(perpetual.getBytes("UTF-8"));

//尾部分割符
os.write("\r\n---------------------146043902153--\r\n\r\n".getBytes());
os.flush();
os.close();

InputStream is = huct.getInputStream();
byte[] by = new byte[is.available()];
is.read(by);
System.out.println(new String(by));//返回資料