How do you Programmatically Download a Webpage in Java

前端 未结 11 2034
无人共我
无人共我 2020-11-22 11:20

I would like to be able to fetch a web page\'s html and save it to a String, so I can do some processing on it. Also, how could I handle various types of compr

11条回答
  •  长情又很酷
    2020-11-22 11:55

    I used the actual answer to this post (url) and writing the output into a file.

    package test;
    
    import java.net.*;
    import java.io.*;
    
    public class PDFTest {
        public static void main(String[] args) throws Exception {
        try {
            URL oracle = new URL("http://www.fetagracollege.org");
            BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
    
            String fileName = "D:\\a_01\\output.txt";
    
            PrintWriter writer = new PrintWriter(fileName, "UTF-8");
            OutputStream outputStream = new FileOutputStream(fileName);
            String inputLine;
    
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                writer.println(inputLine);
            }
            in.close();
            } catch(Exception e) {
    
            }
    
        }
    }
    

提交回复
热议问题