FileUtils.copyUrlToFile not working

I have a pretty simple question: I'm trying to download 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 simple":

 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 the file is empty, which I am doing wrong.

+6
source share
1 answer

Just do 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 

This is because there is a redirect when you click on the URL. It redirects to https. Browsers receive a response, and response code 302 is then redirected to the specified URL, but FileUtility cannot process it.

To confirm the use of firebug or the chrome developers tool , press F12 and go to the Network tab.

+2
source

Source: https://habr.com/ru/post/973085/


All Articles