欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java微信公眾平臺(tái)之素材管理

 更新時(shí)間:2018年05月07日 11:04:11   作者:Phil_Jing  
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)之素材管理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

微信素材管理和群發(fā)這塊文檔對(duì)Java很不友好。本文只對(duì)新增臨時(shí)素材,新增永久素材做介紹,其余獲取、刪除、修改自行補(bǔ)充

公眾號(hào)經(jīng)常有需要用到一些臨時(shí)性的多媒體素材的場(chǎng)景,例如在使用接口特別是發(fā)送消息時(shí),對(duì)多媒體文件、多媒體消息的獲取和調(diào)用等操作,是通過(guò)media_id來(lái)進(jìn)行的。素材管理接口對(duì)所有認(rèn)證的訂閱號(hào)和服務(wù)號(hào)開(kāi)放。

素材的限制

圖片(image): 2M,支持PNG\JPEG\JPG\GIF格式
語(yǔ)音(voice):2M,播放長(zhǎng)度不超過(guò)60s,支持AMR\MP3格式
視頻(video):10MB,支持MP4格式
縮略圖(thumb):64KB,支持JPG格式

一、新增臨時(shí)素材

接口:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE,再傳一個(gè)媒體文件類型,可以是圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb)。

1、訂閱號(hào)和服務(wù)號(hào)要通過(guò)認(rèn)證
2、臨時(shí)素材media_id是可復(fù)用的

3、媒體文件在微信后臺(tái)保存時(shí)間為3天,即3天后media_id失效。

/** 
 * 上傳臨時(shí)素材(本地) 
 * 
 * @param accessToken 
 * @param type 
 *   媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb) 
 * @param path 
 *   圖片路徑 
 * @return 
 */ 
 
public static UploadMediasResult uploadTempMediaFile(String accessToken, String type, String path) { 
 UploadMediasResult result = null; 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", type); 
 try { 
  String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_TEMP_MEDIA_TYPE_URL, 
    params, path); 
  result = JsonUtil.fromJsonString(json, UploadMediasResult.class); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return result; 
} 
 
/** 
 * 上傳臨時(shí)素材(網(wǎng)絡(luò)) 
 * 
 * @param accessToken 
 * @param type 
 *   媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb) 
 * @param path 
 *   圖片路徑 
 * @return 
 */ 
public static UploadMediasResult uploadTempMedia(String accessToken, String type, String path) { 
 UploadMediasResult result = null; 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", type); 
 try { 
  String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_TEMP_MEDIA_TYPE_URL, params, 
    path, 0, 0); 
  result = JsonUtil.fromJsonString(json, UploadMediasResult.class); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return result; 
} 

二、新增永久素材

接口:https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE,媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video,例外)和縮略圖(thumb)

/** 
 * 上傳永久素材(本地) 
 * 
 * @param accessToken 
 * @param type 
 *   媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb) 
 * @return 
 */ 
public static UploadMediasResult uploadForeverMediaFile(String accessToken, String type, String path) { 
 UploadMediasResult result = null; 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", type); 
 try { 
  String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, 
    params, path); 
  result = JsonUtil.fromJsonString(json, UploadMediasResult.class); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return result; 
} 
 
/** 
 * 上傳永久素材(網(wǎng)絡(luò)) 
 * 
 * @param accessToken 
 * @param type 
 *   媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb) 
 * @return 
 */ 
public static UploadMediasResult uploadForeverMedia(String accessToken, String type, String path) { 
 UploadMediasResult result = null; 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", type); 
 try { 
  String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, params, 
    path, 0, 0); 
  result = JsonUtil.fromJsonString(json, UploadMediasResult.class); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return result; 
} 

 新增永久視頻素材需特別注意,在上傳視頻素材時(shí)需要POST另一個(gè)表單,id為description,包含素材的描述信息title和introduction,內(nèi)容格式為JSON

/** 
 * 上傳永久素材(video) 
 * 
 * @param accessToken 
 * @return 
 */ 
public static String uploadForeverMediaFile(String accessToken, String title, String introduction, String path) { 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", "video"); 
 String mediaId = null; 
 try { 
  String json = HttpsUploadVideoMediaFile(SystemConfig.POST_METHOD, 
    WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, params, path, title, introduction); 
  mediaId = JsonUtil.fromJsonString(json, "media_id"); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return mediaId; 
} 
 
/** 
 * 上傳永久素材(video,網(wǎng)絡(luò)) 
 * 
 * @param accessToken 
 * @return 
 */ 
public static String uploadForeverMedia(String accessToken, String title, String introduction, String path) { 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 params.put("type", "video"); 
 String mediaId = null; 
 try { 
  String json = HttpsUploadVideoMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_FOREVER_MEDIA_TYPE_URL, 
    params, path, title, introduction, 0, 0); 
  mediaId = JsonUtil.fromJsonString(json, "media_id"); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return mediaId; 
} 

三、新增永久圖文素材

接口:https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN,post信息參見(jiàn)UploadNewsMedia 實(shí)體類

 對(duì)于常用的素材,開(kāi)發(fā)者可通過(guò)本接口上傳到微信服務(wù)器,永久使用.

1、永久圖片素材新增后,將帶有URL返回給開(kāi)發(fā)者,開(kāi)發(fā)者可以在騰訊系域名內(nèi)使用(騰訊系域名外使用,圖片將被屏蔽)。
2、公眾號(hào)的素材庫(kù)保存總數(shù)量有上限:圖文消息素材、圖片素材上限為5000,其他類型為1000。
3、圖文消息的具體內(nèi)容中,微信后臺(tái)將過(guò)濾外部的圖片鏈接,圖片url需通過(guò)"上傳圖文消息內(nèi)的圖片獲取URL"接口上傳圖片獲取。
4、"上傳圖文消息內(nèi)的圖片獲取URL"接口所上傳的圖片,不占用公眾號(hào)的素材庫(kù)中圖片數(shù)量的5000個(gè)的限制,圖片僅支持jpg/png格式,大小必須在1MB以下。
5、圖文消息支持正文中插入自己帳號(hào)和其他公眾號(hào)已群發(fā)文章鏈接的能力。

/** 
 * 上傳永久圖文消息的素材 
 * 
 * @param accessToken 
 *   授權(quán)token 
 * @param entity 
 *   圖文消息對(duì)象 
 * @return 
 */ 
public static UploadMediasResult uploadNewsMedia(String accessToken, List<UploadNewsMedia> entity) { 
 UploadMediasResult result = null; 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 // post 提交的參數(shù) 
 TreeMap<String, List<UploadNewsMedia>> dataParams = new TreeMap<String, List<UploadNewsMedia>>(); 
 dataParams.put("articles", entity); 
 String data = JsonUtil.toJsonString(dataParams); 
 String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, 
   WechatConfig.UPLOAD_FOREVER_NEWS_MEDIA_URL, params, data); 
 result = JsonUtil.fromJsonString(json, UploadMediasResult.class); 
 return result; 
} 

四、上傳圖文消息內(nèi)的圖片獲取URL

接口:https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN

本接口所上傳的圖片不占用公眾號(hào)的素材庫(kù)中圖片數(shù)量的5000個(gè)的限制。圖片僅支持jpg/png格式,大小必須在1MB以下,此接口返回的url就是上傳圖片的URL,可放置圖文消息中使用。

/** 
 * 上傳圖文消息內(nèi)的圖片獲取URL(本地) 
 * 
 * @param accessToken 
 * @param path 
 * @return 
 */ 
public static String uploadImgMediaFile(String accessToken, String path) { 
 TreeMap<String, String> params = new TreeMap<>(); 
 params.put("access_token", accessToken); 
 String url = null; 
 try { 
  String json = HttpsUploadMediaFile(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_IMG_MEDIA_URL, params, 
    path); 
  url = JsonUtil.fromJsonString(json, "url"); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return url; 
} 
 
/** 
 * 上傳圖文消息內(nèi)的圖片獲取URL(網(wǎng)絡(luò)) 
 * 
 * @param accessToken 
 * @param path 
 * @return 
 */ 
public static String uploadImgMedia(String accessToken, String path) { 
 TreeMap<String, String> params = new TreeMap<String, String>(); 
 params.put("access_token", accessToken); 
 String url = null; 
 try { 
  String json = HttpsUploadMedia(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_IMG_MEDIA_URL, params, path, 0, 
    0); 
  url = JsonUtil.fromJsonString(json, "url"); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 return url; 
} 

五、部分工具類

配置類

public static final String UPLOAD_IMG_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"; 
public static final String UPLOAD_FOREVER_NEWS_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news"; 
public static final String UPLOAD_TEMP_MEDIA_TYPE_URL = "https://api.weixin.qq.com/cgi-bin/media/upload"; 
public static final String UPLOAD_FOREVER_MEDIA_TYPE_URL = "https://api.weixin.qq.com/cgi-bin/material/add_material"; 

上傳圖文消息素材返回類

package com.phil.wechat.msg.model.media; 
 
/** 
 * 上傳圖文消息素材返回的結(jié)果 
 * @author phil 
 * @date 2017年9月20日 
 * 
 */ 
public class UploadMediasResult { 
 private String type; // 媒體文件類型,分別有圖片(image)、語(yǔ)音(voice)、視頻(video)和縮略圖(thumb),次數(shù)為news,即圖文消息 
 private String media_id; // 媒體文件/圖文消息上傳后獲取的唯一標(biāo)識(shí) 
 private String created_at; // 媒體文件上傳時(shí)間 
} 

上傳圖文消息素材實(shí)體類

package com.phil.wechat.msg.model.media; 
 
import java.io.Serializable; 
 
/** 
 * 上傳圖文消息素材實(shí)體類 
 * @author phil 
 * @date 2017年9月20日 
 */ 
public class UploadNewsMedia implements Serializable { 
 
 private static final long serialVersionUID = 6551817058101753854L; 
 private String thumb_media_id; // 圖文消息縮略圖的media_id,可以在基礎(chǔ)支持-上傳多媒體文件接口中獲得 
 private String author; // 圖文消息的作者 
 private String title; // 圖文消息的標(biāo)題 
 private String content_source_url; // 圖文消息點(diǎn)擊閱讀原文的鏈接 
 private String content; // 圖文消息頁(yè)面的內(nèi)容,支持HTML標(biāo)簽 
 private String digest; // 圖文消息的描述 
 private int show_conver_pic; // 是否顯示為封面 1表示顯示為封面 0 不顯示為封面 
} 

上傳方法

/** 
 * 上傳媒體文件(本地) 
 * 
 * @param method 
 *   請(qǐng)求方法 GET/POST 
 * @param path 
 *   api的路徑 
 * @param param 
 *   api參數(shù) 
 * @param mediaPath 
 *   待上傳的image/music 的path 
 * @return 
 * @throws Exception 
 */ 
public static String HttpsUploadMediaFile(String method, String path, Map<String, String> param, String mediaPath) 
  throws Exception { 
 String result = null; 
 URL url = new URL(setParmas(param, path, "")); 
 OutputStream output = null; 
 DataInputStream inputStream = null; 
 try { 
  File file = new File(mediaPath); 
  if (!file.isFile() || !file.exists()) { 
   throw new IOException("file is not exist"); 
  } 
  HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
  con.setDoInput(true); 
  con.setDoOutput(true); 
  con.setUseCaches(false); 
  con.setRequestMethod(SystemConfig.POST_METHOD); 
  // 設(shè)置請(qǐng)求頭信息 
  con.setRequestProperty("Connection", "Keep-Alive"); 
  con.setRequestProperty("Charset", SystemConfig.DEFAULT_CHARACTER_ENCODING); 
  // 設(shè)置邊界 
  String boundary = "----------" + System.currentTimeMillis(); 
  con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
  // 請(qǐng)求正文信息 
  // 第一部分 
  output = new DataOutputStream(con.getOutputStream()); 
  IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write(("Content-Disposition: form-data;name=\"media\"; filename=\"" + file.getName() + "\"\r\n") 
    .getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write( 
    "Content-Type:application/octet-stream\r\n\r\n".getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), 
    output); 
  // IOUtils.write(("Content-Type: "+ fileExt + "\r\n\r\n").getBytes(), output); 
  // 文件正文部分 
  // 把文件已流文件的方式 推入到url中 
  inputStream = new DataInputStream(new FileInputStream(file)); 
  IOUtils.copy(inputStream, output); 
  // 結(jié)尾部分 
  IOUtils.write(("\r\n--" + boundary + "--\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  output.flush(); 
  result = inputStreamToString(con.getInputStream()); 
 } catch (MalformedURLException e) { 
  e.printStackTrace(); 
 } catch (ProtocolException e) { 
  e.printStackTrace(); 
 } catch (IOException e) { 
  throw new IOException("read data error"); 
 } finally { 
  IOUtils.closeQuietly(output); 
  IOUtils.closeQuietly(inputStream); 
 } 
 return result; 
} 
 
/** 
 * 上傳媒體文件(不能本地) 
 * 
 * @param method 
 *   請(qǐng)求方法 GET/POST 
 * @param path 
 *   api的路徑 
 * @param param 
 *   api參數(shù) 
 * @param mediaPath 
 *   待上傳的image/music 的path 
 * @param connTime 
 *   連接時(shí)間 默認(rèn)為5000 
 * @param readTime 
 *   讀取時(shí)間 默認(rèn)為5000 
 * @return 
 * @throws Exception 
 */ 
public static String HttpsUploadMedia(String method, String path, Map<String, String> param, String mediaPath, 
  int connTime, int readTime) throws Exception { 
 String result = ""; 
 URL url = new URL(setParmas(param, path, "")); 
 OutputStream output = null; 
 BufferedInputStream inputStream = null; 
 try { 
  String boundary = "----"; 
  HttpURLConnection conn = getConnection(method, url); 
  conn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime); 
  conn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime); 
  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
  output = conn.getOutputStream(); 
  URL mediaUrl = new URL(mediaPath); 
  if (mediaUrl != null) { 
   HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection(); 
   mediaConn.setDoOutput(true); 
   mediaConn.setUseCaches(false); 
   mediaConn.setRequestMethod(SystemConfig.GET_METHOD); 
   mediaConn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime); 
   mediaConn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime); 
   String connType = mediaConn.getContentType(); 
   // 獲得文件擴(kuò)展 
   String fileExt = getFileExt(connType); 
   IOUtils.write(("--" + boundary + "\r\n").getBytes(), output); 
   IOUtils.write(("Content-Disposition: form-data; name=\"media\"; filename=\"" + getFileName(mediaPath) 
     + "\"\r\n").getBytes(), output); 
   IOUtils.write(("Content-Type: " + fileExt + "\r\n\r\n").getBytes(), output); 
   inputStream = new BufferedInputStream(mediaConn.getInputStream()); 
   IOUtils.copy(inputStream, output); 
   IOUtils.write(("\r\n----" + boundary + "--\r\n").getBytes(), output); 
   mediaConn.disconnect(); 
   // 獲取輸入流 
   result = inputStreamToString(conn.getInputStream()); 
  } 
 } catch (MalformedURLException e) { 
  e.printStackTrace(); 
 } catch (ProtocolException e) { 
  e.printStackTrace(); 
 } catch (IOException e) { 
  e.printStackTrace(); 
 } finally { 
  IOUtils.closeQuietly(output); 
  IOUtils.closeQuietly(inputStream); 
 } 
 return result; 
} 
 
/** 
 * 上傳Video媒體文件(本地) 
 * 
 * @param method 
 *   請(qǐng)求方法 GET/POST 
 * @param path 
 *   api的路徑 
 * @param param 
 *   api參數(shù) 
 * @param mediaPath 
 *   待上傳的voide 的path 
 * @param title 
 *   視頻標(biāo)題 
 * @param introduction 
 *   視頻描述 
 * @return 
 * @throws Exception 
 */ 
public static String HttpsUploadVideoMediaFile(String method, String path, Map<String, String> param, 
  String mediaPath, String title, String introduction) throws Exception { 
 String result = null; 
 URL url = new URL(setParmas(param, path, "")); 
 OutputStream output = null; 
 DataInputStream inputStream = null; 
 try { 
  File file = new File(mediaPath); 
  if (!file.isFile() || !file.exists()) { 
   throw new IOException("file is not exist"); 
  } 
  HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
  con.setDoInput(true); 
  con.setDoOutput(true); 
  con.setUseCaches(false); 
  con.setRequestMethod(SystemConfig.POST_METHOD); 
  // 設(shè)置請(qǐng)求頭信息 
  con.setRequestProperty("Connection", "Keep-Alive"); 
  con.setRequestProperty("Charset", SystemConfig.DEFAULT_CHARACTER_ENCODING); 
  // 設(shè)置邊界 
  String boundary = "----------" + System.currentTimeMillis(); 
  con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
  // 請(qǐng)求正文信息 
  // 第一部分 
  output = new DataOutputStream(con.getOutputStream()); 
  IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write(("Content-Disposition: form-data;name=\"media\"; filename=\"" + file.getName() + "\"\r\n") 
    .getBytes(), output); 
  IOUtils.write("Content-Type: video/mp4 \r\n\r\n".getBytes(), output); 
  // 文件正文部分 
  // 把文件已流文件的方式 推入到url中 
  inputStream = new DataInputStream(new FileInputStream(file)); 
  IOUtils.copy(inputStream, output); 
  // 結(jié)尾部分 
  IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n" 
    .getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write(("{\"title\":\"" + title + "\",\"introduction\":\"" + introduction + "\"}") 
    .getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
  IOUtils.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), 
    output); 
  output.flush(); 
  result = inputStreamToString(con.getInputStream()); 
 } catch (MalformedURLException e) { 
  e.printStackTrace(); 
 } catch (ProtocolException e) { 
  e.printStackTrace(); 
 } catch (IOException e) { 
  throw new IOException("read data error"); 
 } finally { 
  IOUtils.closeQuietly(output); 
  IOUtils.closeQuietly(inputStream); 
 } 
 return result; 
} 
 
/** 
 * 上傳Video媒體文件(網(wǎng)絡(luò)) 
 * 
 * @param method 
 *   請(qǐng)求方法 GET/POST 
 * @param path 
 *   api的路徑 
 * @param param 
 *   api參數(shù) 
 * @param mediaPath 
 *   待上傳的voide 的path 
 * @param title 
 *   視頻標(biāo)題 
 * @param introduction 
 *   視頻描述 
 * @param connTime 
 *   連接時(shí)間 默認(rèn)為5000 
 * @param readTime 
 *   讀取時(shí)間 默認(rèn)為5000 
 * @return 
 * @throws Exception 
 */ 
public static String HttpsUploadVideoMedia(String method, String path, Map<String, String> param, String mediaPath, 
  String title, String introduction, int connTime, int readTime) throws Exception { 
 String result = null; 
 URL url = new URL(setParmas(param, path, "")); 
 OutputStream output = null; 
 BufferedInputStream inputStream = null; 
 try { 
  String boundary = "----"; 
  HttpURLConnection conn = getConnection(method, url); 
  conn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime); 
  conn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime); 
  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
  output = conn.getOutputStream(); 
  URL mediaUrl = new URL(mediaPath); 
  if (mediaUrl != null) { 
   HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl.openConnection(); 
   mediaConn.setDoOutput(true); 
   mediaConn.setUseCaches(false); 
   mediaConn.setRequestMethod(SystemConfig.GET_METHOD); 
   mediaConn.setConnectTimeout(connTime == 0 ? DEFAULT_CONNTIME : connTime); 
   mediaConn.setReadTimeout(readTime == 0 ? DEFAULT_UPLOAD_READTIME : readTime); 
   IOUtils.write(("--" + boundary + "\r\n").getBytes(), output); 
   IOUtils.write(("Content-Disposition: form-data; name=\"media\"; filename=\"" + getFileName(mediaPath) 
     + "\"\r\n").getBytes(), output); 
   IOUtils.write("Content-Type: video/mp4 \r\n\r\n".getBytes(), output); 
   inputStream = new BufferedInputStream(mediaConn.getInputStream()); 
   IOUtils.copy(inputStream, output); 
   // 結(jié)尾部分 
   IOUtils.write(("--" + boundary + "\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
   IOUtils.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n" 
     .getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
   IOUtils.write(("{\"title\":\"" + title + "\",\"introduction\":\"" + introduction + "\"}") 
     .getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), output); 
   IOUtils.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes(SystemConfig.DEFAULT_CHARACTER_ENCODING), 
     output); 
   mediaConn.disconnect(); 
   // 獲取輸入流 
   result = inputStreamToString(conn.getInputStream()); 
  } 
 } catch (MalformedURLException e) { 
  e.printStackTrace(); 
 } catch (ProtocolException e) { 
  e.printStackTrace(); 
 } catch (IOException e) { 
  throw new IOException("read data error"); 
 } finally { 
  IOUtils.closeQuietly(output); 
  IOUtils.closeQuietly(inputStream); 
 } 
 return result; 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java+selenium實(shí)現(xiàn)自動(dòng)化打開(kāi)頁(yè)面的方法

    java+selenium實(shí)現(xiàn)自動(dòng)化打開(kāi)頁(yè)面的方法

    今天小編就為大家分享一篇java+selenium實(shí)現(xiàn)自動(dòng)化打開(kāi)頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Java動(dòng)態(tài)加載類示例詳解

    Java動(dòng)態(tài)加載類示例詳解

    這篇文章主要給大家介紹了關(guān)于Java動(dòng)態(tài)加載類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • 帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹

    帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹

    這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • RocketMQ整合SpringBoot實(shí)現(xiàn)生產(chǎn)級(jí)二次封裝

    RocketMQ整合SpringBoot實(shí)現(xiàn)生產(chǎn)級(jí)二次封裝

    本文主要介紹了RocketMQ整合SpringBoot實(shí)現(xiàn)生產(chǎn)級(jí)二次封裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 用java的spring實(shí)現(xiàn)一個(gè)簡(jiǎn)單的IOC容器示例代碼

    用java的spring實(shí)現(xiàn)一個(gè)簡(jiǎn)單的IOC容器示例代碼

    本篇文章主要介紹了用java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的IOC容器示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • Spring配置類源碼分析詳解

    Spring配置類源碼分析詳解

    這篇文章主要介紹了Spring配置類解析源碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • Spring的Bean容器介紹

    Spring的Bean容器介紹

    今天小編就為大家分享一篇關(guān)于Spring的Bean容器介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • spring-boot整合dubbo:Spring-boot-dubbo-starter

    spring-boot整合dubbo:Spring-boot-dubbo-starter

    這篇文章主要介紹了spring-boot整合dubbo:Spring-boot-dubbo-starter的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-05-05
  • 詳解spring mvc中url-pattern的寫法

    詳解spring mvc中url-pattern的寫法

    這篇文章主要介紹了spring mvc中url-pattern的寫法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Java實(shí)現(xiàn)向數(shù)組里添加元素

    Java實(shí)現(xiàn)向數(shù)組里添加元素

    這篇文章主要介紹了Java實(shí)現(xiàn)向數(shù)組里添加元素方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評(píng)論