How do I save a file downloaded with HttpClient into a specific folder
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();
Download file with Apache HttpClient
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();
}
Download a file with HTTPClient in Java
Oh my god I finally got something that worked. Ok. So apparently HTTPClient can only handle 2 responses before it starts to bug out, according to here: Why does me use HttpClients.createDefault() as HttpClient singleton instance execute third request always hang
So instead, I changed my code to just get a login response, then get the excel file as a response and then quit. I also added some timeout configurations and also changed order from Exporting the file first and then Consuming the entity. I used a separate 2nd response and 2nd entity. That seemed to have helped a bit too? I'm guessing.
import java.util.List;
import java.util.ArrayList;
import org.apache.http.*;
import java.io.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.*;
import org.apache.http.message.*;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.entity.*;
public class hcFeb {
public static void main (String[] args) throws ClientProtocolException, IOException {
//Set up Cookie settings and also Timeout settings
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
int CONNECTION_TIMEOUT = 80000;
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
.setConnectionRequestTimeout(CONNECTION_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT)
.setSocketTimeout(CONNECTION_TIMEOUT)
.build();
//Set up HttpClient
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setDefaultCookieStore(cookieStore).disableContentCompression().build();
HttpGet httpGet = new HttpGet("http://website");
CloseableHttpResponse response = httpclient.execute(httpGet);
//Create Post request to log into the website
HttpPost httpPost = new HttpPost("http://loginwebsite");
//Login to website
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("user","username"));
nvps.add(new BasicNameValuePair("password","password"));
nvps.add(new BasicNameValuePair("button", "Login"));
nvps.add(new BasicNameValuePair("task", "extlogin"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpclient.execute(httpPost);
try{
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} finally{
}
//Send request for Excel file and download it.
String link = "http://website.com/uri?ActSts=Edit&task=filter&Field=Plant";
HttpGet get = new HttpGet(link);
//maybe create new response
HttpResponse response2;
try{
response2 = httpclient.execute(get,context);
System.out.println(response2.getStatusLine());
HttpEntity entity1 = response2.getEntity();
if (entity1 != null) {
System.out.println("Entity isn't null");
InputStream is = entity1.getContent();
String filePath = "C:\\Users\\windowsUserName\\Downloads\\WODETAIL_List.xls";
FileOutputStream fos = new FileOutputStream(new File(filePath));
byte[] buffer = new byte[5600];
int inByte;
while((inByte = is.read(buffer)) > 0)
fos.write(buffer,0,inByte);
is.close();
fos.close();
System.out.println("Excel File recieved");
EntityUtils.toString(response2.getEntity());
EntityUtils.consume(entity1);
}
} catch (ConnectionPoolTimeoutException e){
//response.close();
System.out.println(e.getMessage());
} catch (IOException e){
System.out.println(e.getMessage());
}
}
}
How can I download a file using a simple HttpClient example?
It works fine on my machine if I change the file path to a valid path and add all of the libraries it needs to the classpath.
String filePath = "d:\\test.zip";
Libraries:
commons-codec-1.6.jar
commons-logging-1.1.1.jar
fluent-hc-4.2.3.jar
httpclient-4.2.3.jar
httpclient-cache-4.2.3.jar
httpcore-4.2.2.jar
httpmime-4.2.3.jar
Is it possible to download files like PDF with HttpClient?
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.
}
Angular: How to download a file from HttpClient?
Try something like this:
type: application/ms-excel
/**
* used to get file from server
*/
this.http.get(`${environment.apiUrl}`,{
responseType: 'arraybuffer',headers:headers}
).subscribe(response => this.downLoadFile(response, "application/ms-excel"));
/**
* Method is use to download file.
* @param data - Array Buffer data
* @param type - type of the document.
*/
downLoadFile(data: any, type: string) {
let blob = new Blob([data], { type: type});
let url = window.URL.createObjectURL(blob);
let pwa = window.open(url);
if (!pwa || pwa.closed || typeof pwa.closed == 'undefined') {
alert( 'Please disable your Pop-up blocker and try again.');
}
}
How to download a file from another GET call using Apache Http?
Ok, Lol my bad, this happens when you don't control properly the Try-catch with Resources. Just had to move some code inside of the Try block:
if(repObj != null) {
String requestId = repObj.requestId;
String exportId = repObj.exports.get(0).id;
HttpGet get = new HttpGet("http://localhost:8081/jasperserver/rest_v2/reportExecutions/"+requestId+"/exports/"+exportId+"/outputResource");
HttpEntity content;
String name;
String filetype;
try (CloseableHttpResponse chres = httpClient.execute(get,httpContext);) {
StatusLine status = chres.getStatusLine();
name = chres.getFirstHeader("Content-Disposition").getValue().split(";")[1];
filetype = chres.getFirstHeader("Content-Type").getValue();
content = chres.getEntity();
if(status.getStatusCode()==200) {
response.setContentType(filetype);
response.setHeader("Content-disposition", name);
response.setHeader("Content-Length", String.valueOf(content.getContentLength()));
try (InputStream in = content.getContent();
OutputStream out = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int numBytesRead;
while ((numBytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, numBytesRead);
}
}
httpClient.close();
}
}
}
} else {
// Error
}
I was getting 0kb because the InputStream was closing it early.
Related Topics
Time Complexity of Hashmap Methods
Multiple Scanner Inputs (Java)
Use Only One Bufferedreader Object to Read Multiple Files in Java
Http Post Request With Content Type Application/X-Www-Form-Urlencoded Not Working in Spring
Spring MVC - Get Httpservletresponse Body
How to Create Multiple Objects Through Loops in Java
Prevent Empty Values on String Split
How to Conditionally Enable or Disable Scheduled Jobs in Spring
Convert to Int from String of Numbers Having Comma
How to Specify the Required Java Version in a Gradle Build
How to Get the Path of Src/Test/Resources Directory in Junit
Gson Expected Begin_Array But Was String At Line 1 Column 62
Splitting a String Containing Special Characters in to Individual Characters
Using Nested While Loop to Print Pyramid of Stars
Kafka Consumer in Java Not Consuming Messages