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 ?