优酷接口的使用

@author 达内--教学支持与督导部--软件研发团队 * @version v1.0 2014-07-07 * */ public class YouKuUtil { private static ResumeDao resumeDao;//求职秀Dao static{ ApplicationContext  app = new ClassPathXmlApplicationContext("applicationContext.xml"); resumeDao = (ResumeDao)app.getBean("resumeDao"); } /** * 读取优酷提供的查询视频状态的json内容 * @param client_id 应用Key * @param video_id 视频ID * @return json对象 */ public static Object getVideoStatus(String client_id,String video_id){ //构造HttpClient的实例 HttpClient httpClient = new HttpClient(); // 创建 GET方法的实例 GetMethod getMethod = new GetMethod("https://openapi.youku.com/v2/videos/show_basic.json?client_id="+client_id+"&video_id="+video_id+"&random="+(new Random().nextInt(1000))); //使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { //执行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { return null; } //读取内容 byte[] responseBody = getMethod.getResponseBody(); //处理内容 return new String(responseBody,"utf-8"); } catch (HttpException e) { //发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); return null; } catch (IOException e) { // 发生网络异常 e.printStackTrace(); return null; } finally { //释放连接 getMethod.releaseConnection(); } } /** * 删除优酷平台上的视频 * @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; } } }

转载于:https://my.oschina.net/surenpi/blog/481858

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值