socket 从网上下载到本地

来源:互联网 发布:php微信菜单消息推送 编辑:程序博客网 时间:2024/04/29 04:32

package com.socket;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class Test {

public static void main(String[] args) {    // 把网上的图片 复制到 本地 没有就创建    // try {    // URL url=new    // URL("http://f.hiphotos.baidu.com/image/w%3D310/sign=2aea3c45cafcc3ceb4c0cf32a244d6b7/a50f4bfbfbedab646eb5fda7f536afc378311e95.jpg");    // File fileimg=new File("d:\\11.jpg");    // if(!fileimg.exists()){    // fileimg.createNewFile();    // OutputStream os=new FileOutputStream(fileimg);    // InputStream is=url.openStream();    // byte[]array=new byte[1024];    // int i=is.read(array);    // while(i!=-1){    // os.write(array, 0, i);    // i=is.read(array);    // }    // os.flush();    // os.close();    // is.close();    // }    //    // } catch (MalformedURLException e) {    // // TODO Auto-generated catch block    // e.printStackTrace();    // } catch (IOException e) {    // // TODO Auto-generated catch block    // e.printStackTrace();    // }    //打开某一网站    try {        URL url = new URL("http://baidu.com");        InputStream is = url.openStream();        BufferedReader br = new BufferedReader(new InputStreamReader(is));        String line = br.readLine();        while (line != null) {            System.out.println(line);        line=br.readLine();        }        br.close();        is.close();    } catch (MalformedURLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }}

}

0 0
原创粉丝点击