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

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

概述提前致谢..首先,我要这个用于android.我必须发送一个带有zip文件的http发布请求,该zip文件包含一个包含名称列表的xml文件.现在,根据我发送的名称列表,服务器将向我发送一个zip文件的二进制数据,我必须将该二进制数据(响应)另存为一个zip文件.问题是,当我将此二进制数据另存为z

提前致谢..

首先,我要这个用于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设备可能会用完内存.

总结

以上是内存溢出为你收集整理的java-将httppost响应中的zip文件下载并保存为ANDROID中的二进制数据全部内容,希望文章能够帮你解决java-将httppost响应中的zip文件下载并保存为ANDROID中的二进制数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088659.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存