如何使用HttpUnit进行上传文件测试

 例子代码

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值