how to get url html contents to string in java

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

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

Tags:

Html

Url

File Io