集册 Java实例教程 从URL获取

从URL获取

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

351
从URL获取
/** n o w j a   v  a . c o m - 时  代  Java 提供 **/


//package com.nowjava;

import java.io.*;

import java.net.*;


public class Main {

    public static String getFrom(String URL) throws Exception {

        StringBuffer res = new StringBuffer("");

        try {

            URL url = new URL(URL);

            URLConnection postUrlConnection = url.openConnection();

            postUrlConnection.setDoOutput(true);

            postUrlConnection.setUseCaches(false);

            postUrlConnection.setRequestProperty("Accept",

                    "application/rdf+xml");


            InputStream postInputStream = postUrlConnection

                    .getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(/**来自 n  o  w  j  a  v  a . c o m**/

                    postInputStream));


            char[] buff = new char[1024 * 100];

            int n;

            int i = 0;

            do {

                n = in.read(buff);

                if (n != -1) {

                    res.append(new String(
展开阅读全文