在Android中使用SOAP从服务器下载大文件?我正在使用kso ap2库。大于30MB的文件会抛出OutOfMemoryError

问题描述 投票:0回答:1
 HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_GET_FILE);
androidHttpTransport.debug = true;
try {
    androidHttpTransport.call(SOAP_ACTION_GET_FILE, envelope);

        StringBuffer theresponseString = new StringBuffer(androidHttpTransport.responseDump);

    if( !(envelope.bodyIn instanceof SoapFault)) {

        {
            byte[] arrby = Base64.decode((String) ((SoapObject) envelope.bodyIn).getPropertyAsString("file").toString(), Base64.DEFAULT);
            Log.e("Byte", arrby.toString());

            FileOutputStream output;

                output = new FileOutputStream(dir + "/" + requiredFiles.getPath());
                output.write(arrby);

            output.flush();
            output.close();
            status = true;

    }
}
catch (SoapFault fault)
{
    status = false;
    fault.printStackTrace();
}
catch (Exception e) {
    status = false;
    e.printStackTrace();
}

上面的代码用于下载文件,当文件大小大于30MB时,它会在byte []处抛出OutOfMemoryError。

请指导我修复解决方案。提前谢谢...... !!!

android android-ksoap2
1个回答
0
投票

您可以按块大小下载文件,并将其附加到每个块大小的同一文件中。以下是示例代码:

  @Override
protected Void doInBackground1(RequiredFiles... params) {

    FileOutputStream output;
    requiredFiles = params[0];

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_GET_FILE);
    // adding method property here serially
    request.addProperty("serverKey", serverKey); 
    request.addProperty("hardwareKey", CommonFunctions.getUUID(context).toString()); 
    request.addProperty("fileId", requiredFiles.getId());
    request.addProperty("fileType", requiredFiles.getType());
    request.addProperty("chunkOffset", requiredFiles.getDefaultChuckOFFSet());
    request.addProperty("chuckSize", requiredFiles.getDefaultChuckSize());

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.implicitTypes = true;
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);


    String URL_GET_FILE = baseURL + UrlApiResource.GET_FILE;
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_GET_FILE);
    androidHttpTransport.debug = true;
    try {
        androidHttpTransport.call(SOAP_ACTION_GET_FILE, envelope);

        if( !(envelope.bodyIn instanceof SoapFault)) {

            byte[] arrby = Base64.decode((String) ((SoapObject) envelope.bodyIn).getPropertyAsString("file"), Base64.DEFAULT);

            try {

                output = new FileOutputStream(dir + "/temp_" + requiredFiles.getPath(), true);
                output.write(arrby);
                output.flush();
                output.close();
            }catch (IOException ex){
                ex.printStackTrace();

            }//catch
        }//if

        status = true;
    } catch (SoapFault fault) {
        status = false;
        fault.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
    catch (Exception e) {
        status = false;
        e.printStackTrace();
        }
        return null; 
}
© www.soinside.com 2019 - 2024. All rights reserved.