9

I have a html file stored on the server. I have the URL path something like this: <https://localhost:9443/genesis/Receipt/Receipt.html >

I want to read the contents of this html file which would contain tags, from the url i.e. the source code of the html file.

How am I supposed to do this? This is a server side code and can't have a browser object and I am not sure using a URLConnection would be a good option.

What should be the best solution now?

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
Peyush Goel
  • 383
  • 2
  • 3
  • 13
  • `String content = org.apache.commons.io.IOUtils.toString(new Url("https://localhost:9443/genesis/Receipt/Receipt.html"), "utf8");` – Klitos Kyriacou Feb 03 '16 at 11:05

5 Answers5

13
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
public class URLContent {
    public static void main(String[] args) {
        try {
            // get URL content
            
            String a = "http://localhost:8080//TestWeb/index.jsp";
            URL url = new URL(a);
            URLConnection conn = url.openConnection();
 
            // open the stream and put it into BufferedReader
            BufferedReader br = new BufferedReader(
                               new InputStreamReader(conn.getInputStream()));
 
            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                System.out.println(inputLine);
            }
            br.close();
 
            System.out.println("Done");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
Vaibs
  • 2,018
  • 22
  • 29
3

Resolved it using spring added the bean to the spring config file

  <bean id = "receiptTemplate" class="org.springframework.core.io.ClassPathResource">
    <constructor-arg value="/WEB-INF/Receipt/Receipt.html"></constructor-arg>
  </bean>

then read it in my method

        // read the file into a resource
        ClassPathResource fileResource =
            (ClassPathResource)context.getApplicationContext().getBean("receiptTemplate");
        BufferedReader br = new BufferedReader(new FileReader(fileResource.getFile()));
        String line;
        StringBuffer sb =
            new StringBuffer();

        // read contents line by line and store in the string
        while ((line =
            br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();
Peyush Goel
  • 383
  • 2
  • 3
  • 13
1
import java.net.*;
import java.io.*;

//...

URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStream reader = url.openStream();
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
0

For exemple :

        URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
        URLConnection con = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String l;
        while ((l=in.readLine())!=null) {
            System.out.println(l);
        }

You could use the inputstream in other ways, not just printing it.

Of course if you have the path to the local file, you can also do

  InputStream in = new FileInputStream(new File(yourPath));
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • this is a server side code and I was refraining from using URLConnection. Not sure if it would be a good approach. What do you reckon? – Peyush Goel Jun 18 '12 at 16:39
  • This is not server side code, why are you refraining from using URLConnection? – Chris Dargis Jun 18 '12 at 16:41
  • 1
    If you know where are your files locally, you also can use `FileInputStream` (see extended answer). – Denys Séguret Jun 18 '12 at 16:42
  • the code I am working on is a server side code. I am actually writing a service which would read the receipt template, also stored on the web server(and available through the url) and process it. – Peyush Goel Jun 18 '12 at 16:45
  • If the files you are looking for are not on your server, I don't see any other way besides connecting to the URL. However, as mentioned above, if the files are local you can use a regular `FileInputStream`. – Chris Dargis Jun 18 '12 at 16:48
  • The file is on a web server and the code I am working on would also be deployed on the webserver. Authentication and connection management won't be an issue. Only thing is I am not able to read the contents. I am trying the URLConnection method but I am not sure it would be the best approach, though I am not so sure of that as well – Peyush Goel Jun 18 '12 at 16:51
  • @PeyushGoel: If the files are local you should be able to use `FileInputStream`, provided you have the full path. – Chris Dargis Jun 18 '12 at 16:54
  • url connection is throwing certification exception.... probably this is a development environment and won't occur in a production environment but i should have a better approach. Is there a way to read it in a file, fileReader, fileInputStream? remember I have a url to the html file – Peyush Goel Jun 18 '12 at 17:16
  • 1
    You need to know where is the root of your html files so that you can use FileInputStream. – Denys Séguret Jun 18 '12 at 17:17
  • 1
    From the URL, you can read it with URLConnection. But it's more advisable to determine where are your files and read it using FileInputStream. – Denys Séguret Jun 18 '12 at 17:48
  • is there a way to do it using spring config? – Peyush Goel Jun 18 '12 at 18:04
0

Simplest way in my opinion is to use IOUtils

import com.amazonaws.util.IOUtils;
...

String uri = "https://localhost:9443/genesis/Receipt/Receipt.html";
String fileContents = IOUtils.toString(new URL(uri).openStream());
System.out.println(fileContents);
Haris Bouchlis
  • 2,366
  • 1
  • 20
  • 35