如何将使用HttpClient下载的文件保存到特定文件夹中
我正在尝试使用HttpClient下载PDF文件。我可以获取文件,但是我不确定如何将字节转换为PDF并将其存储在系统中的某个位置
我有以下代码,如何将其存储为PDF?
public ???? getFile(String url) throws ClientProtocolException, IOException{ HttpGet httpget = new HttpGet(url);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
InputStream inputStream = entity.getContent();
// How do I write it?
}
return null;
}
回答:
InputStream is = entity.getContent();String filePath = "sample.txt";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
您还可以使用BufferedOutputStream和BufferedInputStream来加快下载速度:
BufferedInputStream bis = new BufferedInputStream(entity.getContent());String filePath = "sample.txt";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();
以上是 如何将使用HttpClient下载的文件保存到特定文件夹中 的全部内容, 来源链接: utcz.com/qa/425555.html