隨筆 - 3, 文章 - 0, 評論 - 9, 引用 - 0
          數(shù)據(jù)加載中……

          2006年7月18日

          java應(yīng)用程序從網(wǎng)上下載文件

          java應(yīng)用程序與網(wǎng)絡(luò)通訊一直是我的弱項(xiàng),想補(bǔ)一補(bǔ),就從這一篇開始吧!
          以從網(wǎng)上下載一個(gè)圖象為例:

          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 ;
          ????}
          }


          小試了一下,還不錯(cuò)!

          posted @ 2006-08-14 09:03 nick 閱讀(1729) | 評論 (6)編輯 收藏

          java.beans.PropertyChangeSupport小解!

               摘要: java.beans.PropertyChangeSupport是一個(gè)實(shí)用工具類,支持綁定該屬性的bean能夠使用該類.當(dāng)執(zhí)行setName操作時(shí),會(huì)觸發(fā)firePropertyChange事件, 因?yàn)镻ropertyChangeSupportTest和PropertyChangeSupport綁定,那么在BeanTestListener中,因?yàn)樘砑永镏С诸惖谋O(jiān)聽器,所以應(yīng)該觸發(fā)propertyChange事件, 并且會(huì)根據(jù)evt.getPropertyName()來獲得你在BeanTest中觸發(fā)事件是綁定的屬性名稱.........
            閱讀全文

          posted @ 2006-07-18 14:02 nick 閱讀(3867) | 評論 (4)編輯 收藏

          主站蜘蛛池模板: 余干县| 衡阳县| 黄浦区| 二连浩特市| 瑞昌市| 海宁市| 托克逊县| 望谟县| 车险| 隆子县| 凤阳县| 兰州市| 大兴区| 东港市| 海阳市| 渝中区| 乐亭县| 巩义市| 武汉市| 梁河县| 达日县| 拉萨市| 筠连县| 滦平县| 灵丘县| 乳源| 乐至县| 黄山市| 汉沽区| 揭西县| 郑州市| 阳原县| 盈江县| 古丈县| 鹰潭市| 林西县| 五原县| 广安市| 四子王旗| 施甸县| 保德县|