如何使用HttpUnit进行上传文件测试
2008-08-29 13:37
891 查看
例子代码
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);
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);
相关文章推荐
- 如何使用HttpUnit进行上传文件测试
- 转 使用 HttpClient 4 进行文件上传
- 如何使用 Spring MVC 进行单文件与多文件上传的三种情况
- 使用uploadify插件进行多文件上传时如何传递表单参数
- HttpClient使用详解 (如何上传文件到文件服务器)
- 如何使用ASIHTTPRequest从iOS端向服务器上传文件?
- 使用HttpUrlConnection进行post请求上传文件
- 在android中使用HttpURLConnection进行文件上传
- 使用Loadrunner进行文件的上传及代码测试
- linux下如何使用sftp命令进行文件上传和下载
- linux下如何使用sftp命令进行文件上传和下载
- 使用HttpUrlConnection进行post请求上传文件
- 【自动化测试】利用Jmeter对Http接口的文件上传进行测试
- 如何使用Git客户端进行文件(代码)上传
- 如何使用Git客户端图形化进行文件(代码)上传
- linux下如何使用sftp命令进行文件上传和下载
- 如何使用PuTTY进行文件上传
- 软件测试中使用HttpUnit进行集成测试(一)
- 使用 BASH 作为 CGI 进行 HTTP 文件上传
- 使用HttpUrlConnection进行post请求上传文件