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;
 
publicclassURLContent {
    publicstaticvoidmain(String[] args) {
        try {
            // get URL contentStringa="http://localhost:8080//TestWeb/index.jsp";
            URLurl=newURL(a);
            URLConnectionconn= url.openConnection();
 
            // open the stream and put it into BufferedReaderBufferedReaderbr=newBufferedReader(
                               newInputStreamReader(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

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

then read it in my method

// read the file into a resourceClassPathResourcefileResource=
            (ClassPathResource)context.getApplicationContext().getBean("receiptTemplate");
        BufferedReaderbr=newBufferedReader(newFileReader(fileResource.getFile()));
        String line;
        StringBuffersb=newStringBuffer();

        // read contents line by line and store in the stringwhile ((line =
            br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        return sb.toString();

Solution 3:

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

//...URLurl=newURL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStreamreader= url.openStream();

Solution 4:

For exemple :

URLurl=newURL("https://localhost:9443/genesis/Receipt/Receipt.html");
        URLConnectioncon= url.openConnection();
        BufferedReaderin=newBufferedReader(newInputStreamReader(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

InputStreamin=newFileInputStream(newFile(yourPath));

Solution 5:

Simplest way in my opinion is to use IOUtils

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

Stringuri="https://localhost:9443/genesis/Receipt/Receipt.html";
StringfileContents= IOUtils.toString(newURL(uri).openStream());
System.out.println(fileContents);

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