![](/img/trans.png)
[英]How to save a downloaded file into a particular folder in Java selenium?
[英]How do I save a file downloaded with HttpClient into a specific folder
我正在尝试使用 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();
只是为了记录,有更好(更简单)的方法来做同样的事情
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);
}
}
}
或者使用 fluent API,如果你更喜欢它
Request.Get("http://host/stuff").execute().saveContent(myFile);
这是一个使用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();
}
使用依赖org.apache.httpcomponents:fluent-hc
:
Request.Get(url).execute().saveContent(file);
请求来自org.apache.http.client.fluent.Request
。
就我而言,我需要一个流,这同样简单:
inputStream = Request.Get(url).execute().returnContent().asStream();
如果您使用的是 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);
}
打开一个FileOutputStream
并将inputStream
的字节保存到它。
您还可以使用 Apache http 客户端 fluent API
Executor executor = Executor.newInstance().auth(new HttpHost(host), "user", "password");
executor.execute(Request.Get(url.toURI()).connectTimeout(1000)).saveContent("C:/temp/somefile");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.