集册 Java实例教程 下载URL并返回字符串

下载URL并返回字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

396
下载URL并返回String


//package com.nowjava;
/**
n o w j a v a . c o m - 时代Java 提供 
**/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;


public class Main {


    public static String download(URL url) throws IOException {

        String data = "";

        InputStream is = null;

        HttpURLConnection connection = null;

        try {

            connection = (HttpURLConnection) url.openConnection();
            /**来自 
             n o w j a v a . c o m - 时代Java**/

            is = connection.getInputStream();

            BufferedReader br = new BufferedReader(

                    new InputStreamReader(is));


            StringBuffer sb = new StringBuffer();

            String line = "";

            while ((line = br.readLine()) != null) {

                sb.append(line);

            }

            data = sb.toString();

        } catch (IOException e) {

            e.printStackTr
展开阅读全文