8

I have a html file stored on the server. I have the URL path something like this: <https://localhost:9443/genesis/Receipt/Receipt.html >

I want to read the contents of this html file which would contain tags, from the url i.e. the source code of the html file.

How am I supposed to do this? This is a server side code and can't have a browser object and I am not sure using a URLConnection would be a good option.

What should be the best solution now?

4

5 に答える 5

12
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();
        }
    }
}
于 2013-02-04T12:19:45.650 に答える
2

spring を使用して解決し、Bean を spring config ファイルに追加しました

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

それから私の方法でそれを読んでください

        // 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();
于 2012-06-19T18:01:35.507 に答える
0

例えば ​​:

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

印刷するだけでなく、他の方法で入力ストリームを使用できます。

もちろん、ローカルファイルへのパスがある場合は、次のこともできます

  InputStream in = new FileInputStream(new File(yourPath));
于 2012-06-18T16:35:20.483 に答える
0
import java.net.*;
import java.io.*;

//...

URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStream reader = url.openStream();
于 2012-06-18T16:37:55.113 に答える