优酷文档写的一般 有的地址拼写错误 而且有的地方返回服务器域名,需要将其转换为ip方可使用。
普通用户基本模式就是 先通过一个优酷链接,让用户授权,获取授权文件再一步步上传。详细可以参考http://open.youku.com/docs/upload_client_chinese.html
文件上传需要注意使用httpbody传文件流 使用httpclient的 post方式 get方式body 均无法上传,返回crc校验失败,md5校验失败
校验方法如下:public staticString getMD5Str(String str) {
MessageDigest messageDigest= null;try{
messageDigest= MessageDigest.getInstance("MD5");
System.out.println(str);
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
}catch(NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
System.exit(-1);
e.printStackTrace();
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}byte[] byteArray =messageDigest.digest();
StringBuffer md5StrBuff= newStringBuffer();for (int i = 0; i < byteArray.length; i++) {if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(Integer.toHexString(0xFF &byteArray[i]));elsemd5StrBuff.append(Integer.toHexString(0xFF &byteArray[i]));
}returnmd5StrBuff.toString();
}public static String getCrc32(byte[]b){
CRC32 crc32=newCRC32();
ByteArrayInputStream bis=newByteArrayInputStream(b);
CheckedInputStream cis=newCheckedInputStream(bis, crc32);
String crc= null;try{while (cis.read() != -1) {
}
crc=Long.toHexString(crc32.getValue()).toLowerCase();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returncrc;
}
httpclient文档很多,但是使用httpbody传递的文档基本没有找到,最后没有办法使用urlconnectionpublic static String upload_slice(JSONObject json,byte[]b){
URL url;try{
url= new URL("http://"+CmdUtil.getIpByUrl(json.getString("upload_server_uri"))+"/gupload/upload_slice" +
"?upload_token="+json.getString("upload_token")+"&slice_task_id="+json.getString("slice_task_id")+"&offset="+json.getString("offset")+"&length="+json.getString("length")+"&crc="+json.getString("crc")+"&hash="+json.getString("hash")
);
URLConnection urlConnection=url.openConnection();//设置doOutput属性为true表示将使用此urlConnection写入数据
urlConnection.setDoOutput(true);//定义待写入数据的内容类型,我们设置为application/x-www-form-urlencoded类型
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");//得到请求的输出流对象
BufferedOutputStream out = newBufferedOutputStream(urlConnection.getOutputStream());//把数据写入请求的Body
out.write(b);
out.flush();
out.close();//从服务器读取响应
InputStream inputStream =urlConnection.getInputStream();
String encoding=urlConnection.getContentEncoding();
String body=IOUtils.toString(inputStream, encoding);returnbody;
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}return null;
}
以上代码有个人的编码风格,很多人用httpclient传参数,一般都会将参数封装到一个map里面,然后遍历map取参数,这里使用了json
cmdutil是通过ping获取ip地址
上传成功后会返回id,但是在个人空间中看不到视频,据说是优酷需要审核。