Skip to content Skip to sidebar Skip to footer

How To Get Url Html Contents To String In Java

I have a html file stored on the server. I have the URL path something like this: I want to read the contents of this h

Solution 1:

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();
        }
    }
}

Solution 2:

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();

Solution 3:

import java.net.*;
import java.io.*;

//...

URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStream reader = url.openStream();

Solution 4:

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));

Solution 5:

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);

Post a Comment for "How To Get Url Html Contents To String In Java"