java http get请求下载文件_java-将httppost响应中的zip文件下载并保存为...

提前致谢..

首先,我要这个用于android.

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

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

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

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

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

这是我的代码.

private class sendMissingImagesToServer extends

AsyncTask {

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

{

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值