隨筆 - 3, 文章 - 0, 評論 - 9, 引用 - 0

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          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 on 2006-08-14 09:03 nick 閱讀(1727) 評論(6)  編輯  收藏 所屬分類: java基礎

          評論

          # re: java應用程序從網上下載文件  回復  更多評論   

          <script>alert("hello")</script>
          2006-09-07 10:08 | aa

          # re: java應用程序從網上下載文件  回復  更多評論   

          <script>document.title("hello world")</script>
          2006-09-07 10:11 | aa

          # re: java應用程序從網上下載文件  回復  更多評論   

          xiexie
          2006-10-02 09:54 | kk

          # re: java應用程序從網上下載文件  回復  更多評論   

          安全可靠
          2006-11-01 19:05 |

          # re: java應用程序從網上下載文件  回復  更多評論   

          奶奶內
          2006-12-02 20:31 | 奶奶內

          # re: java應用程序從網上下載文件  回復  更多評論   

          xiexieni
          2006-12-12 20:27 | sunxiuqi

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 上虞市| 红原县| 元阳县| 招远市| 安丘市| 桦川县| 陕西省| 弋阳县| 屏山县| 武乡县| 桐梓县| 迁西县| 漳平市| 齐河县| 隆子县| 博湖县| 商都县| 南和县| 西乌| 青田县| 依兰县| 仙游县| 浙江省| 和顺县| 香港| 沙雅县| 潞西市| 遂平县| 泰州市| 石泉县| 加查县| 肥城市| 新巴尔虎左旗| 阳泉市| 灵台县| 怀柔区| 大余县| 湘西| 齐齐哈尔市| 西乡县| 博湖县|