站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题根据图片的url将图片保存到本地

根据图片的url将图片保存到本地

摘要: 根据图片的url将图片保存到本地

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="514" height="86" align="center" valign="top"> 根据图片的url将图片保存到本地。

一、代码
import java.io.*;

import java.net.*;

public class SaveImage {

    public static void main(String args[]) {

    DataInputStream di = null;

    FileOutputStream fo = null;

    byte [] b = new byte[1]; 

      

    try {

       // input

       URL url = new URL(args[0]);

       URLConnection urlConnection = url.openConnection();

       urlConnection.connect();

       di = new DataInputStream(urlConnection.getInputStream());

       // output

       fo = new FileOutputStream(args[1]);

       //  copy the actual file

       //   (it would better to use a buffer bigger than this)

       while(-1 != di.read(b,0,1))  {

         fo.write(b,0,1);

       }

       di.close(); 

       fo.close();               

     }

     catch (Exception ex) {

         ex.printStackTrace();

         System.exit(1);

     }

     System.out.println("done."); 

   }

}

二、运行方法,如:

C:\java>java SaveImage http://www.java3z.com/cwbwebhome/images/logo.gif logo.gif
done.
</td> <td width="170" valign="top"> </td> </tr> <tr> <td height="20" colspan="2">
   
</td> </tr> </table> </td> </tr> <tr>


↑返回目录
前一篇: 将数据库操作封装到Javabean
后一篇: 怎样制作*_zh_CN.properties中文资源文件