博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

流水孟春

以前使用的博客 webvista.cublog.cn
lib.cublog.cn
java读远程文件

package test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;

/**
 * 写了个和php的 file_get_contents("http://bbs.my.com/test/test8.php?a=1"); 一样的功能的东东
 *
 * @author 流水孟春
 */

public class F {

    public static void main(String[] args) throws Exception {
        String url = "http://bbs.xinbbw.com/test/test8.php?a=1";

        URL resource = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;

        resource = new URL(url);
        httpUrl = (HttpURLConnection) resource.openConnection();
        httpUrl.connect();
        StringBuffer sb = new StringBuffer();

        BufferedReader in = null;
        in = new BufferedReader(new InputStreamReader(httpUrl.getInputStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sb.append(inputLine);
        }
        String str = new String(sb.toString().getBytes(), "utf-8");
        System.out.println(str);
        
        httpUrl.disconnect();
        
        bis = null;
        httpUrl = null;
        resource = null;
    }
}

发表于: 2008-05-03,修改于: 2008-05-06 14:48,已浏览136次,有评论0条 推荐 投诉

给我留言
版权所有 ChinaUnix.net 页面生成时间:0.67444