Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUtils.copyUrlToFile is not working

I have fairly basic question i'm trying to download a PDF from this URL using java:

http://kundservice.svd.se/ui/templates/HttpHandler/PDFTidningen/PDFTidningen.ashx?date=2014-07-27&file=SVD_D_BILAGA_2014-07-27.pdf&mac=92267fcd3c75feff13154ba66870a523&free=True

here is my code "very basic":

public class Main {

private static final String PATH = "C:\\project\\DownloadTest\\src\\main\\resources\\tmp\\";
private static final String FILENAME = "data.pdf";
private static final String SvDPDFURL = "http://kundservice.svd.se/ui/templates/HttpHandler/PDFTidningen/PDFTidningen.ashx?date=2014-07-27&file=SVD_D_BILAGA_2014-07-27.pdf&mac=92267fcd3c75feff13154ba66870a523&free=True";

public static void main(String[] args) throws Exception{
    File file = new File(PATH + FILENAME);
    URL url = new URL(SvDPDFURL);
    FileUtils.copyURLToFile(url, file);
 }
}

the problem is that file is empty, what i'm i doing wrong.

like image 868
Gideon Oduro Avatar asked Jul 30 '14 08:07

Gideon Oduro


1 Answers

Just make it https instead of http.

https://kundservice.svd.se/ui/templates/HttpHandler/PDFTidningen/PDFTidningen.ashx?date=2014-07-27&file=SVD_D_BILAGA_2014-07-27.pdf&mac=92267fcd3c75feff13154ba66870a523&free=True

It is happening because there is a redirect when you hit the url. It redirects to https. Browsers get the response and the response code is 302 then redirects to the given url but the FileUtility can not handle that.

To confirm use firebug or chrome developers tool by hitting F12 and go to Network tab.

like image 117
bitkot Avatar answered Sep 30 '22 18:09

bitkot