例子代码
String sip_appkey = "test_app003";// app_id
String sip_apiname = "alitest.ali-001-upload";
String sip_appsecret = "secret_app003";
String sessionid = "sddddeee";
String url = prepareUrl(sip_appkey, sip_apiname, sip_appsecret,
api_server, sessionid);
WebConversation conversation = new WebConversation();
//if use src_url,code will jump SIP
PostMethodWebRequest request = new PostMethodWebRequest(url);
request.setMimeEncoded(true);
File f=new File(filein_path);
UploadFileSpec[] ufs = {new UploadFileSpec(f,"image/gif")};
request.setParameter("image[file]", ufs);
WebResponse resp = conversation.getResponse(request);
System.out.printf("%s/n", resp.getText());
try{
byte[] buf = new byte[1024];
OutputStream bufferOut = new BufferedOutputStream(new FileOutputStream(fileout_path));
InputStream respis = resp.getInputStream();
int numRead = 0;
do {
numRead = respis.read(buf, 0, buf.length);
if (numRead > 0) {
bufferOut.write(buf, 0, numRead);
}
} while (numRead >= 0);
bufferOut.flush();
bufferOut.close();
System.out.printf("Src file's MD5 is %s/nRecevied file's MD5 is %s/n.",getHash(filein_path,"MD5"),getHash(fileout_path,"MD5"));
//uoload will fail when SIP limit 0KB
switch(assertequal){
case 0:assertTrue(!getHash(filein_path,"MD5").equals(getHash(fileout_path,"MD5")));break;
case 1:assertTrue(getHash(filein_path,"MD5").equals(getHash(fileout_path,"MD5")));break;
default :break;
}
}
catch(IOException e){System.out.printf("%s",e.getMessage());}
关键特性:在上传的时候需要设置如下代码
request.setMimeEncoded(true);