随笔 - 3, 文章 - 0, 评论 - 9, 引用 - 0
数据加载中……

2006年8月14日

java应用程序从网上下载文件

java应用程序与网络通讯一直是我的弱项,想补一补,就从这一篇开始吧!
以从网上下载一个图象为例:

import  java.io.BufferedInputStream;
import  java.io.BufferedOutputStream;
import  java.io.File;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.net.URL;
import  java.net.URLConnection;

public   class  DownloadFormURL {

    
public   static   void  main(String[] args) {
        
try  {
            
new  DownloadFormURL().downloadFile( " http://127.0.0.1:8080/image/f.jpg " new  File( " F:/image.jpg " ));
        } 
catch  (IOException e) {
            e.printStackTrace();
        }
    }

    
public   void  downloadFile(String sourceURL, File targetFile)  throws  IOException {

        URL url 
=   new  URL(sourceURL);
        URLConnection connection 
=  url.openConnection();
        java.io.InputStream inputStream 
=  connection.getInputStream();
        FileOutputStream outputStream 
=   new  FileOutputStream(targetFile);
        BufferedInputStream in 
=   null ;
        BufferedOutputStream out 
=   null ;
        
byte  buffer[]  =   new   byte [ 8192 ];
        
try  {
            in 
=   new  BufferedInputStream(inputStream, buffer.length);
            out 
=   new  BufferedOutputStream(outputStream, buffer.length);
            
int  total  =   0 ;
            
for  ( int  bytesRead  =   0 ; (bytesRead  =  in.read(buffer))  !=   - 1 ;) {
                out.write(buffer, 
0 , bytesRead);
                total 
+=  bytesRead;
            }

        } 
finally  {
            in.close();
            out.close();
        }
        
return ;
    }
}


小试了一下,还不错!

posted @ 2006-08-14 09:03 nick 阅读(1702) | 评论 (6)编辑 收藏

2006年7月18日

java.beans.PropertyChangeSupport小解!

     摘要: java.beans.PropertyChangeSupport是一个实用工具类,支持绑定该属性的bean能够使用该类.当执行setName操作时,会触发firePropertyChange事件, 因为PropertyChangeSupportTest和PropertyChangeSupport绑定,那么在BeanTestListener中,因为添加里支持类的监听器,所以应该触发propertyChange事件, 并且会根据evt.getPropertyName()来获得你在BeanTest中触发事件是绑定的属性名称.........
  阅读全文

posted @ 2006-07-18 14:02 nick 阅读(3829) | 评论 (4)编辑 收藏

2006年7月17日

我的Blog终于开通了!

一直想像别的网友一样,在网上尽情挥洒自己的才华,表达自己的喜怒哀乐,记录自己生活的点点滴滴...... 今天,我的blog开通了。首先,当然要祝愿自己一下,希望自己有一个美好的明天! 同时,也希望广大网友时常光临自己的blog小屋。 ( *_* )

posted @ 2006-07-17 08:38 nick 阅读(347) | 评论 (0)编辑 收藏