Sugo

          Goof off ? no way: never
          posts - 20, comments - 8, trackbacks - 73, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          實現(xiàn)自己的下載程序

          Posted on 2007-01-26 14:04 sugo 閱讀(323) 評論(0)  編輯  收藏

          ????? 實現(xiàn)了基本的網(wǎng)絡(luò)圖片、mp3、影片下載,原理為首先根據(jù)要下載的圖片(mp3、影片)的url,將它的信息寫入一個臨時文件。然后再從這個臨時文件將信息導(dǎo)入到真正你想要存儲的圖片(mp3、影片)。第一步為網(wǎng)絡(luò)到本地,第二步為本地到本地。
          ???????//網(wǎng)絡(luò)到本地代碼:
          ?????? ?java.net.URL l_url = new java.net.URL("http://localhost/rlzy/photo/"+addition); //圖片url地址
          ??????? java.net.HttpURLConnection l_connection = (java.net.HttpURLConnection) l_url.openConnection();
          ??????? int randomnum=(int)(Math.random()*100);
          ??????? System.out.println(randomnum);
          ??????? File f1=new File(photopath+randomnum);
          ??????? FileOutputStream o=new FileOutputStream(f1);
          ??????? l_connection.connect();
          ??????? java.io.InputStream l_urlStream;
          ???????
          ??????? l_urlStream = l_connection.getInputStream();

          ?????? byte b[]=new byte[1000];
          ?????? int n;
          ????????? while( (n=l_urlStream.read(b))!=-1)
          ????????????? {
          ????????? ??? o.write(b,0,n);
          ????????????? }
          ????????? o.close();
          ????????? l_urlStream.close();
          ????????
          ?????????
          ????????? //本地到本地
          ????????? RandomAccessFile random=new RandomAccessFile(f1,"r");
          ????????? long? forthEndPosition=0;
          ???????????File tfolder1=new File("E:/111/);//存儲圖片路徑
          ???????? ? tfolder1.mkdirs();//如果不存在就創(chuàng)建此文件夾,如果沒有這個步驟將會出現(xiàn)FileNotFoundException
          ??????????File f2=new File("e:/111/"+name);
          ?????????????
          ??????????RandomAccessFile random2=new RandomAccessFile(f2,"rw");?
          ????????????
          ????????? random.seek(random.length());
          ???????? ?long endPosition=random.getFilePointer();
          ??????????random.seek(forthEndPosition);
          ??????????long startPoint=random.getFilePointer();
          ????????????while(startPoint<endPosition) //開始拷貝文件信息
          ????????????????? { n=random.readByte();
          ??????????????????? random2.write(n);
          ??????????????????? startPoint=random.getFilePointer();
          ????????????????? }
          ?????????? random2.close();random.close();
          ?????????? f1.delete(); //刪除臨時文件?????
          ????????? 程序?qū)懥艘粋€簡單的GUI界面?,使其看起來更像一個下載器了。如果提高下載速度還需進一步的學(xué)習(xí)和研究。有興趣的朋友可以點擊下載研究
          ????????? http://sugo.gnway.net/test/FreeDown.jar(申請的一個免費二級域名,速度可能不快 :-) ,另外需要sun公司的jre環(huán)境,Windows、Linux下均能運行)
          ?????????


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 同江市| 错那县| 岢岚县| 洛浦县| 满洲里市| 大新县| 彭山县| 凭祥市| 舞阳县| 响水县| 井陉县| 武强县| 石渠县| 谢通门县| 天门市| 安达市| 商南县| 大渡口区| 安平县| 廉江市| 保亭| 锦州市| 南宁市| 武强县| 莱芜市| 石门县| 红桥区| 沁水县| 霍林郭勒市| 体育| 潜山县| 林州市| 淮阳县| 丹江口市| 蒙自县| 文化| 吉木萨尔县| 天气| 贵德县| 临澧县| 耒阳市|