Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file with Apache HttpClient

what im trying to do is to download a file with httpclient. At the moment my code is the following.

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(downloadURL);     


    HttpResponse response = client.execute(request);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        FileOutputStream fos = new FileOutputStream("C:\\file");
        entity.writeTo(fos);
        fos.close();
    }

My download URL is something like that: http://example.com/file/afz938f348dfa3

As you can see there is no extension to the file (in the url at least) however, when i go to the url with a normal browser, it does download the file "asdasdaasda.txt" or "asdasdasdsd.pdf" (the name is different from the url and the extenstion is not always the same, depends on what im trying to download).

My http response looks like this:

Date: Mon, 29 May 2017 14:57:14 GMT Server: Apache/2.4.10 Content-Disposition: attachment; filename="149606814324_testfile.txt" Accept-Ranges: bytes Cache-Control: public, max-age=0 Last-Modified: Mon, 29 May 2017 14:29:06 GMT Etag: W/"ead-15c549c4678-gzip" Content-Type: text/plain; charset=UTF-8 Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 2554 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive

How can i do so my java code automatically download the file with the good name and extension in a specific folder ?

like image 421
retArdos Avatar asked Apr 23 '26 04:04

retArdos


1 Answers

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();
}
like image 199
Ovidiu Dolha Avatar answered Apr 25 '26 17:04

Ovidiu Dolha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!