1

I am trying to read file from web url

method 1

    try {
        String fileUrl = "https://github.com/DhyanandraSingh/appointment-mt/StudentInternship.xlsx";
        URL url = new URI(fileUrl).toURL();
        FileInputStream excelFile = new FileInputStream(new File(url.toString()));
}catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

but it throw error:

java.io.FileNotFoundException: https:\github.com\DhyanandraSingh\appointment-mt\StudentInternship.xlsx (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138) 

but when i read any local file it will work.

method 2

i also tried but result is same fileNotFoundException

URLConnection connection = new URL(fileUrl).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
dhyanandra singh
  • 1,071
  • 2
  • 18
  • 38
  • You cannot read a file from a remote server through https using a java.io.File. Read this https://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153 to perform an HTTP get to get the file. – StephaneM Oct 11 '17 at 11:55

0 Answers0