how to get url html contents to string in java

后端 未结 5 1877
北恋
北恋 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 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();
            }
    
        }
    }
    

提交回复
热议问题