如何使用KSOAP2从webservice获取文件的一部分并创建zip?

时间:2016-02-28 00:43:23

标签: java android web-services zip ksoap2

我需要从Web服务中获取部分文件。 Web服务让我拆分文件。我发送的参数(我需要的部分和字节大小)先前已经知道总共有多少部分和它们各自的重量。

如何使用 KSOAP2 获取这些部分,然后创建 .zip

byte[] bytess = SomeDecode(MyMethodWS (10, 1024)); //example Part 10 of 15

1 个答案:

答案 0 :(得分:0)

我选择了一个基本解决方案:通过“for”连接和断开连接,询问位和字节到ws。鉴于要求的最后一部分应该很好地计算(numberBytes)。

FileOutputStream fos = new FileOutputStream(file,true); //Important that 'true'.

int numberBytes =  1024; //if numberBytes < 'sizefile' then = sizefile
int numberPart = 0; //

int parts = sizefile/numberBytes; // if the result is decimal must add +1

for(int b=0;b<parts;b++){

    //Calculate the part to be ordered: 0...numberBytes+1 ...n+1 ...n+1
    numberPart = functionCalculate(numberBytes,sizefile,b);

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo p1 = new PropertyInfo();
    pi.setName("part");
    pi.setValue(numberPart);
    pi.setType(int.class);
    Request.addProperty(p1);

    PropertyInfo p2 = new PropertyInfo();
    p2.setName("bytess");
    p2.setValue(numberBytes);
    p2.setType(int.class);
    Request.addProperty(p2);

    Blah blah ksoap code…
    Blah blah ksoap code…

    try{

        Blah blah ksoap code…
        Blah blah ksoap code…

        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();                         
        bytes = response.toString().getBytes(“UTF-8”);                      
        fos.write(bytes,0,bytes.length);//It is a zip. no need to build a zip    
    }catch(...
}//end for

close fos