import java.io.*; public class fileupload { /** * @param fileupload * @exception IOException * @see 读一个URL的图片存到本地某路径下 */ public static void main(String[] args) throws IOException{ //读取文件 String s1 = "http://www.baidu.com/img/baidu_logo.gif"; java.io.InputStream is = null; //定义一个输入流。 BufferedInputStream bis = null;//定义一个带缓冲的输入流 。 try{ java.net.URL url = new java.net.URL(s1);//创建一个URL对象。 is = url.openStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。 bis = new java.io.BufferedInputStream(is); }catch(Exception e){ System.out.println(e.toString()); } //写到本地 BufferedOutputStream bos = null; //定义一个带缓冲的输出流。 File file = new File("C:/baidu_logo.gif"); if(!file.exists()){ //测试此抽象路径名表示的文件或目录是否存在。 file.createNewFile(); //创建此抽象路径名表示的文件或目录。 } try{ bos = new BufferedOutputStream(new FileOutputStream(file));; byte[] b = new byte[1024]; //创建字节数组。 while(bis.read(b)!=-1){//输入流中的数据如果还有下一行(!=-1)将继续循环 bos.write(b);//将字节数组写入输出流。 } }catch(Exception e){ System.out.println(e.toString()); }finally{ try{ bos.flush();//刷新此缓冲的输出流。 bis.close(); //关闭此输入流 。 }catch(Exception e){ System.out.println(e.toString()); } } } }