how to get url html contents to string in java

后端 未结 5 1869
北恋
北恋 2021-02-04 17:49

I have a html file stored on the server. I have the URL path something like this:

I want to rea

相关标签:
5条回答
  • 2021-02-04 17:58
    import java.net.*;
    import java.io.*;
    
    //...
    
    URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
    url.openConnection();
    InputStream reader = url.openStream();
    
    0 讨论(0)
  • 2021-02-04 17:58

    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);
    
    0 讨论(0)
  • 2021-02-04 18:00

    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));
    
    0 讨论(0)
  • 2021-02-04 18:03

    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();
    
    0 讨论(0)
  • 2021-02-04 18: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 URLConetent{
        public static void main(String[] args) {
    
            URL url;
    
            try {
                // get URL content
    
                String a="http://localhost:8080//TestWeb/index.jsp";
                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();
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题