package URL;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import org.omg.CORBA.portable.InputStream;
public class URLDemo {
public static void main(String[] args) throws IOException {
Download.download("http://img03.sogoucdn.com/app/a/100520020/ab84b70f33697910f6c92d22a673bb0e","my.jpg", "F:\");
}
}
class Download{
public static void download(String urlstring,String filename,String savepath) throws IOException{
//创建URL定位网络资源
URL url =new URL(urlstring);
// URLConnection con = url.openConnection();
// java.io.InputStream is = con.getInputStream();
//得到URL字节输入流
java.io.InputStream is = url.openStream();
byte[] buff = new byte[1024];
int len = 0;
File file = new File(savepath);
if(!file.exists()){
//创建指定文件夹
file.mkdirs();
}
//传送完整文件路径及文件名给文件输出流用以创建文件
OutputStream os = new FileOutputStream(file.getAbsolutePath()+"\"
+filename);
//为文件写入内容
while((len=is.read(buff))!=-1){
os.write(buff,0,len);
}
os.close();
is.close();
}
}