您的位置:首页 > 其它

socket 从网上下载到本地

2015-08-03 21:03 288 查看
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();
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: