'FileNotFoundException from java spring boot app while using URI.create(url).toURL().openStream()

Through my spring boot app, i am trying to read a url string which contains pdf file by using below code.

InputStream inputStream = null;
inputStream = URI.create(url).toURL().openStream()

url is https://www.sample.com/example.pdf

When it reaches the URI code, it throws the FileNotFoundException in the application server. Same code works fine in my local machine(both mac and windows).

Any idea on the issue fix

Trying to copy the pdf file from the remote url into local folder

Tried both NIO and InputStreamReader as mentioned in the below website

https://www.baeldung.com/java-download-file



Solution 1:[1]

It is resolved. The site which I try to fetch the pdf file is taking sometime to load the pdf in the remote url. Increasing the thread sleeping time to wait until its generated. So issue is resolved

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Soorya2121