编程语言
首页 > 编程语言> > java-将httppost响应中的zip文件下载并保存为ANDROID中的二进制数据

java-将httppost响应中的zip文件下载并保存为ANDROID中的二进制数据

作者:互联网

提前致谢..

首先,我要这个用于android.

我必须发送一个带有zip文件的http发布请求,该zip文件包含一个包含名称列表的xml文件.

现在,根据我发送的名称列表,服务器将向我发送一个zip文件的二进制数据,我必须将该二进制数据(响应)另存为一个zip文件.

问题是,当我将此二进制数据另存为zip文件时,则无法提取该zip.

我认为这也可能是一些字符集问题.我需要将接收到的二进制数据转换为某些字符集,然后将其另存为zip.

请帮助我,我是android新手.任何执行此操作的ASYNC任务示例都将是很好的帮助.

这是我的代码.

private class sendMissingImagesToServer extends
        AsyncTask<String, Integer, byte[]> {

    @Override
    protected byte[] doInBackground(String... params) {
        String uri = params[0];
        try {

            MultipartEntityBuilder entity;
            File f;
            FileBody fb;
            entity = MultipartEntityBuilder.create();

            entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            f = new File(zipImageFile);
            fb = new FileBody(f);
            entity.addPart("orderFile", fb);
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(uri);
            Log.e("Uploload Missing Image URL", "" + uri);
            httppost.setEntity(entity.build());
            HttpResponse response = httpclient.execute(httppost);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer stringBuffer = new StringBuffer();
//              byte[] fileBites=null;
            String line = "";

            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }
            bufferedReader.close();

//              fileBites=stringBuffer.toString().getBytes();
//              Log.e("FILE BITES", fileBites+"=>"+fileBites.length);

            ByteArrayOutputStream bObj = new ByteArrayOutputStream();
            bObj.reset();
            bObj.write(stringBuffer.toString().getBytes());

            return bObj.toByteArray();

//              return stringBuffer.toString();
        } catch (Exception e) {
            return e.toString().getBytes();
        }

    }

    @Override
    protected void onPostExecute(byte[] result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.e("Response From Server", "" + result);
        writeToFile(result);

    }

}

@SuppressWarnings("resource")
private void writeToFile(byte[] data) {
    try {

        FileOutputStream fop = null;
        File file;

        file = new File(AppConstants.DataPath+"/products.zip");
        fop = new FileOutputStream(file);

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        try {            
        fop.write(data);

    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
    unzipImage(AppConstants.DataPath + "/products.zip",
            AppConstants.DataPath);
}catch (Exception E)
{

}
}

解决方法:

读取器无意读取八位位组流.

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

您正在寻找BufferedInputStream.

HttpEntity上的getContent()方法返回一个InputStream.将其包装在BufferedInputStream周围,然后将其写入文件或ByteArrayOutputStream.

        byte[] buffer = new byte[5 * 1024];
        int numRead = -1;
        while( (numRead = bufferedInputStream.read(buffer))!= -1)
        {
            byteArrayOutputStream.write(buffer, 0, numRead);
        }
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();
        byte[] result = byteArrayOutputStream.toByteArray();

为了节省内存,我建议您写入BufferedOutputStream,而不要尝试将字节从流获取到数据结构中.大型zip文件的android设备可能会用完内存.

标签:android-asynctask,zip,binary-data,java,android
来源: https://codeday.me/bug/20191029/1961765.html