Java URL Connection urlToHtm(String word, String findurl, String path)

Here you can find the source of urlToHtm(String word, String findurl, String path)

Description

url To Htm

License

Open Source License

Declaration

public static void urlToHtm(String word, String findurl, String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void urlToHtm(String word, String findurl, String path) {

        URL url = null;/*from   ww  w . j  a va 2 s .  c  o  m*/
        try {
            url = new URL(findurl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        String charset = "utf-8";
        int sec_cont = 1000;
        try {
            URLConnection url_con = url.openConnection();
            url_con.setDoOutput(true);
            url_con.setReadTimeout(10 * sec_cont);
            url_con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            InputStream htm_in = url_con.getInputStream();

            String htm_str = InputStream2String(htm_in, charset);
            saveHtml(path, htm_str);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String InputStream2String(InputStream in_st, String charset) throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(in_st, charset));
        StringBuffer res = new StringBuffer();
        String line = "";
        while ((line = buff.readLine()) != null) {
            res.append(line);
        }
        return res.toString();
    }

    public static void saveHtml(String filepath, String str) {

        try {
            OutputStreamWriter outs = new OutputStreamWriter(new FileOutputStream(filepath, true), "utf-8");
            outs.write(str);
            outs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. url2list(String sURL, String encoding)
  2. urlContents(String urlString)
  3. urlExists(String urlString)
  4. URLExists(URL url, StringBuffer errorMsg)
  5. urlifyMap(Long nid, double latitude, double longitude)
  6. urlToReader(URL url)
  7. urlToString(String url)
  8. urlToText(@Nonnull String url)