优酷接口的使用

/**
* 删除优酷平台上的视频
* @param client_id 应用Key
* @param video_id 视频ID
* @param access_token OAuth2授权
* @return json对象
*/
public static String deleteVideoJson(String client_id, String video_id, String access_token) {
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
String url  =   ” https://openapi.youku.com/v2/videos/destroy.json ” ;
//获取post方法实例
PostMethod postMethod  =   new  PostMethod(url);

// 填入各个表单域的值
NameValuePair[] data = { new NameValuePair(“client_id”, client_id),
new NameValuePair(“access_token”, access_token),
new NameValuePair(“video_id”, video_id) };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try {
// 执行postMethod
httpClient.executeMethod(postMethod);
//打印结果页面
String response =   new String(postMethod.getResponseBodyAsString().getBytes(“8859_1″),”utf-8”);
return response;
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
return null;
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
return null;
}finally {
//释放连接
postMethod.releaseConnection();
}
}

/**
* 上传创建接口,用于提交将要上传视频的信息
* @param client_id 客户Key
* @param access_token 客户口令
* @param filepath 文件路径
* @return json对象
*/
public static Object uploadFile(String client_id,String access_token,String filepath,Resume resume){
File file = new File(filepath);
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//获取文件名
String file_name = file.getName();
//获取文件大小
int file_size = new Long(file.length()).intValue();
//获取文件的md5值
String file_md5 = MD5Util.getFileMD5(file);
// 创建 GET方法的实例
//System.out.println(“文件名–“+file_name+”文件大小–“+file_size+”file的MD5值–“+file_md5);
String title =”Tarena_”+resume.getUserId()+”_”+resume.getId()+”_jobshow”;
String tags = “Tarena”;
String url = “https://openapi.youku.com/v2/uploads/create.json?client_id=”+client_id+”&access_token=”+access_token+”&title=”+title+”&tags=”+tags+”&file_name=”+file_name+”&file_md5=”+file_md5+”&file_size=”+file_size;
System.out.println(url);
GetMethod getMethod = new GetMethod(url);
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try {
//执行getMethod
int statusCode = httpClient.executeMethod(getMethod);

if (statusCode != HttpStatus.SC_OK) {
System.err.println(“Method failed: ” + getMethod.getStatusLine());
}
//读取内容
byte[] responseBody = getMethod.getResponseBody();
//处理内容
System.out.println(“获取json的数据为———“+new String(responseBody,”utf-8″));
return new String(responseBody,”utf-8”);
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println(“Please check your provided http address!”);
e.printStackTrace();
return null;
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
return null;
} finally {
//释放连接
getMethod.releaseConnection();
}
}
/**
* 用于真实上传的接口
* @param client_id 客户Key
* @param access_token 客户口令
* @param filepath 文件路径
* @return json对象
*/
public static Boolean commitUploadFile(String client_id,String access_token,String filepath,String parameter){
//获取jobSoo对象
Resume resume = resumeDao.findResumeByUserId(Integer.parseInt(parameter.split(“_”)[1]));
//构造HttpClient的实例
HttpClient httpClient = new HttpClient();
String url  =   ” https://openapi.youku.com/v2/uploads/commit.json ” ;
//获取post方法实例
PostMethod postMethod  =   new  PostMethod(url);
Object json = uploadFile(client_id,access_token,filepath,resume);
//将字符串转换为json对象
JSONObject obj = JSONObject.fromObject(json);
//将json对象转换为javaBean对象
YouKuJsonBean bean = (YouKuJsonBean) JSONObject.toBean(obj,YouKuJsonBean.class);
//获取上传token
String upload_token = bean.getUpload_token();
// 填入各个表单域的值
NameValuePair[] data = {new NameValuePair(“access_token”, access_token),
new NameValuePair(“client_id”, client_id),
new NameValuePair(“upload_token”, upload_token) };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
try {
// 执行postMethod
httpClient.executeMethod(postMethod);
//打印结果页面
String response =   new String(postMethod.getResponseBodyAsString().getBytes(“8859_1″),”utf-8”);
//{“video_id”:”XNzM4MDQxMTAw”}
//处理数据
String videoId = response.split(“:”)[1].substring(1, response.split(“:”)[1].length()-2);
resume.setVideoId(videoId);
resume.setVideoStatus(0);
//更新jobSoo信息
resumeDao.updateResume(resume);
return true;
} catch (HttpException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println(“Please check your provided http address!”);
e.printStackTrace();
return false;
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
return false;
}
}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注