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();
EDIT:
you can also use BufferedOutputStream and BufferedInputStream for faster download:
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();
Answer from Eng.Fouad on Stack Overflowjava - How do I save a file downloaded with HttpClient into a specific folder - Stack Overflow
java - Download file with Apache HttpClient - Stack Overflow
Download a file with HTTPClient in Java
java - Download zip file using java11 HttpClient.sendAsync() - Stack Overflow
Videos
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();
EDIT:
you can also use BufferedOutputStream and BufferedInputStream for faster download:
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();
Just for the record there are better (easier) ways of doing the same
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);
}
}
}
Or with the fluent API if one likes it better
Request.Get("http://host/stuff").execute().saveContent(myFile);
You can get the file name and extension from your response's content-disposition header
First get the header then parse it for the filename as explained here, i.e:
HttpEntity entity = response.getEntity();
if (entity != null) {
String name = response.getFirstHeader('Content-Disposition').getValue();
String fileName = disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*
1");
FileOutputStream fos = new FileOutputStream("C:\\" + fileName);
entity.writeTo(fos);
fos.close();
}
More formal way would be to use HeaderElements API:
Optional<String> resolveFileName(HttpResponse response) {
return Arrays.stream(response.getFirstHeader("Content-Disposition").getElements())
.map(element -> element.getParameterByName("filename"))
.filter(Objects::nonNull)
.map(NameValuePair::getValue)
.findFirst();
}
Using httpclient is pretty easy. Here's a link to it's tutorial.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urltofetch);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
InputStream inputStream = entity.getContent();
// write the file to whether you want it.
}
Anything you can do with HttpURLConnection you can do, usually better, with HttpClient look through their examples about file transfer and you will see how.