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

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

          評(píng)論

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

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

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

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

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

          xiexie
          2006-10-02 09:54 | kk

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

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

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

          奶奶內(nèi)
          2006-12-02 20:31 | 奶奶內(nèi)

          # re: java應(yīng)用程序從網(wǎng)上下載文件  回復(fù)  更多評(píng)論   

          xiexieni
          2006-12-12 20:27 | sunxiuqi

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 乌恰县| 长乐市| 辛集市| 株洲县| 咸丰县| 郯城县| 丰台区| 顺昌县| 金阳县| 磐安县| 淅川县| 盐亭县| 绵阳市| 荃湾区| 辛集市| 德钦县| 弥渡县| 波密县| 新泰市| 蓬莱市| 佳木斯市| 南汇区| 古田县| 旬阳县| 荆州市| 乐清市| 化隆| 新田县| 肇源县| 汶川县| 葫芦岛市| 吉木萨尔县| 佛坪县| 佳木斯市| 贵州省| 麻江县| 思茅市| 庄浪县| 元江| 砚山县| 灵山县|