天天看点

URL保存本地

try {
            URL url = new URL("https://www.baidu.com");   //想要读取的url地址
            File fp = new File("D:/test.html");
            OutputStream os = new FileOutputStream(fp);          //建立文件输出流
            URLConnection conn = url.openConnection();          //打开url连接
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String urlString = "";
            String current;
            while ((current = in.readLine()) != null) {
                urlString += current;
            }
            os.write(urlString.getBytes());
            os.close();

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