我正在尝试使用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;
}
答案 0 :(得分:39)
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();
答案 1 :(得分:35)
只是为了记录,有更好(更容易)的方法来做同样的事情
sqlserver
或者使用流畅的API,如果你更喜欢它
File myFile = new File("mystuff.bin");
CloseableHttpClient client = HttpClients.createDefault();
try (CloseableHttpResponse response = client.execute(new HttpGet("http://host/stuff"))) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try (FileOutputStream outstream = new FileOutputStream(myFile)) {
entity.writeTo(outstream);
}
}
}
答案 2 :(得分:22)
以下是使用IOUtils.copy()
的简单解决方案:
File targetFile = new File("foo.pdf");
if (entity != null) {
InputStream inputStream = entity.getContent();
OutputStream outputStream = new FileOutputStream(targetFile);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
}
return targetFile;
IOUtils.copy()
很棒,因为它处理缓冲。但是,这种解决方案的可扩展性不是很高:
更具可扩展性的解决方案涉及两个功能:
public void downloadFile(String url, OutputStream target) throws ClientProtocolException, IOException{
//...
if (entity != null) {
//...
InputStream inputStream = entity.getContent();
IOUtils.copy(inputStream, target);
}
}
辅助方法:
public void downloadAndSaveToFile(String url, File targetFile) {
OutputStream outputStream = new FileOutputStream(targetFile);
downloadFile(url, outputStream);
outputStream.close();
}
答案 3 :(得分:2)
如果您使用的是Java 7+,则可以使用本机Files.copy(InputStream in, Path target, CopyOption... options),例如:
HttpEntity entity = response.getEntity();
try (InputStream inputStream = entity.getContent()) {
Files.copy(inputStream, Paths.get(filePathString), StandardCopyOption.REPLACE_EXISTING);
}
答案 4 :(得分:1)
打开FileOutputStream
并将inputStream
中的字节保存到它。
答案 5 :(得分:1)
使用依赖项org.apache.httpcomponents:fluent-hc
:
Request.Get(url).execute().saveContent(file);
请求来自org.apache.http.client.fluent.Request
。
就我而言,我需要一个流,这同样简单:
inputStream = Request.Get(url).execute().returnContent().asStream();
答案 6 :(得分:0)
您还可以使用Apache http客户端流利API
Executor executor = Executor.newInstance().auth(new HttpHost(host), "user", "password");
executor.execute(Request.Get(url.toURI()).connectTimeout(1000)).saveContent("C:/temp/somefile");