首页 > Web开发 > 详细

URL编程

时间:2014-12-16 22:34:09      阅读:357      评论:0      收藏:0      [点我收藏+]

1 统一资源定位符,一个URL的对象,对应互联网上的一个对象,我们可以通过URL

对象调用其相应的方法,将其资源下载

package lianxi1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;

public class TestURL {
@Test
   public void test() throws IOException{
    URL url = new URL("http://127.0.0.1:8080/manager/hello.txt");
    System.out.println(url.getPath());
    System.out.println(url.getPort());
    //1.读取服务器的资源
    InputStream is = url.openStream();
    byte[] b = new byte[20];
    int len;
    while((len=is.read(b))!=-1){
        String str = new String(b,0,len);
        System.out.print(str);
    }
    is.close();
    //2.如果有数据输出
    URLConnection uc = url.openConnection();
    InputStream is2 = uc.getInputStream();
    FileOutputStream os = new FileOutputStream(new File("welcome.txt"));
    byte[] b2 = new byte[20];
    int len2;
    while((len2=is2.read(b2))!=-1){
        os.write(b2,0,len2);
    }
    os.close();
    is2.close();
}
}

URL编程

原文:http://www.cnblogs.com/yjtm53/p/4168189.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!