1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      Android如何實(shí)現(xiàn)簡(jiǎn)單的文件下載與上傳

      這篇文章主要介紹了Android如何實(shí)現(xiàn)簡(jiǎn)單的文件下載與上傳,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

      成都創(chuàng)新互聯(lián)是少有的網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、營(yíng)銷型企業(yè)網(wǎng)站、微信平臺(tái)小程序開發(fā)、手機(jī)APP,開發(fā)、制作、設(shè)計(jì)、買鏈接、推廣優(yōu)化一站式服務(wù)網(wǎng)絡(luò)公司,于2013年開始,堅(jiān)持透明化,價(jià)格低,無套路經(jīng)營(yíng)理念。讓網(wǎng)頁(yè)驚喜每一位訪客多年來深受用戶好評(píng)

      文件下載

      /**
       * 下載服務(wù) IntentService 
       * 生命周期:
       * 1>當(dāng)?shù)谝淮螁?dòng)IntentService時(shí),Android容器
       *  將會(huì)創(chuàng)建IntentService對(duì)象。
       * 2>IntentService將會(huì)在工作線程中輪循消息隊(duì)列,
       *  執(zhí)行每個(gè)消息對(duì)象中的業(yè)務(wù)邏輯。
       * 3>如果消息隊(duì)列中依然有消息,則繼續(xù)執(zhí)行,
       *  如果消息隊(duì)列中的消息已經(jīng)執(zhí)行完畢,
       *  IntentService將會(huì)自動(dòng)銷毀,執(zhí)行onDestroy方法。
       */
      public class DownloadService extends IntentService{
        private static final int NOTIFICATION_ID = 100;
        public DownloadService(){
          super("download");
        }
        public DownloadService(String name) {
          super(name);
        }
        /**
         * 該方法中的代碼將會(huì)在工作線程中執(zhí)行
         * 每當(dāng)調(diào)用startService啟動(dòng)IntentService后,
         * IntentService將會(huì)把OnHandlerIntent中的
         * 業(yè)務(wù)邏輯放入消息隊(duì)列等待執(zhí)行。
         * 當(dāng)工作線程輪循到該消息對(duì)象時(shí),將會(huì)
         * 執(zhí)行該方法。
         */
        protected void onHandleIntent(Intent intent) {
          //發(fā)送Http請(qǐng)求 執(zhí)行下載業(yè)務(wù)
          //1. 獲取音樂的路徑
          String url=intent.getStringExtra("url");
          String bit=intent.getStringExtra("bit");
          String title=intent.getStringExtra("title");
          //2. 構(gòu)建File對(duì)象,用于保存音樂文件
          //   /mnt/sdcard/Music/_64/歌名.mp3
          File targetFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),"_"+bit+"/"+title+".mp3" );         
          if(targetFile.exists()){
            Log.i("info", "音樂已存在");
            return;
          }
          if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
          }
          try {
            sendNotification("音樂開始下載", "音樂開始下載");
            //3. 發(fā)送Http請(qǐng)求,獲取InputStream
            InputStream is = HttpUtils.getInputStream(url);
            //4. 邊讀取邊保存到File對(duì)象中
            FileOutputStream fos = new FileOutputStream(targetFile);
            byte[] buffer = new byte[1024*100];
            int length=0;
            int current = 0;
            int total = Integer.parseInt(intent.getStringExtra("total"));
            while((length=is.read(buffer)) != -1){
              fos.write(buffer, 0, length);
              fos.flush();
              current += length;
              //通知下載的進(jìn)度
              double progress = Math.floor(1000.0*current/total)/10;
              sendNotification("音樂開始下載", "下載進(jìn)度:"+progress+"%");
            }
            //5. 文件下載完成
            fos.close();
            cancelNotification(); //重新出現(xiàn)滾動(dòng)消息
            sendNotification("音樂下載完成", "音樂下載完畢");
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        /**
         * 發(fā)通知
         */
        public void sendNotification(String ticker, String text){
          NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          Notification.Builder builder = new Notification.Builder(this);
          builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("音樂下載")
            .setContentText(text)
            .setTicker(ticker);
          Notification n = builder.build();
          manager.notify(NOTIFICATION_ID, n);
        }
        /**
         * 取消通知
         */
        public void cancelNotification(){
          NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
          manager.cancel(NOTIFICATION_ID);
        }    
      }

      文件上傳

       /** 
         * 上傳文件 
         * @param uploadFile 
         */ 
        private void uploadFile(final File uploadFile) { 
          new Thread(new Runnable() {      
            @Override 
            public void run() { 
              try { 
                uploadbar.setMax((int)uploadFile.length()); 
                String souceid = logService.getBindId(uploadFile); 
                String head = "Content-Length="+ uploadFile.length() + ";filename="+ uploadFile.getName() + ";sourceid="+ 
                  (souceid==null? "" : souceid)+"\r\n"; 
                Socket socket = new Socket("192.168.1.78",7878); 
                OutputStream outStream = socket.getOutputStream(); 
                outStream.write(head.getBytes()); 
                PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());   
                String response = StreamTool.readLine(inStream); 
                String[] items = response.split(";"); 
                String responseid = items[0].substring(items[0].indexOf("=")+1); 
                String position = items[1].substring(items[1].indexOf("=")+1); 
                if(souceid==null){//代表原來沒有上傳過此文件,往數(shù)據(jù)庫(kù)添加一條綁定記錄 
                  logService.save(responseid, uploadFile); 
                } 
                RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "r"); 
                fileOutStream.seek(Integer.valueOf(position)); 
                byte[] buffer = new byte[1024]; 
                int len = -1; 
                int length = Integer.valueOf(position); 
                while(start&&(len = fileOutStream.read(buffer)) != -1){ 
                  outStream.write(buffer, 0, len); 
                  length += len; 
                  Message msg = new Message(); 
                  msg.getData().putInt("size", length); 
                  handler.sendMessage(msg); 
                } 
                fileOutStream.close(); 
                outStream.close(); 
                inStream.close(); 
                socket.close(); 
                if(length==uploadFile.length()) logService.delete(uploadFile); 
              } catch (Exception e) { 
                e.printStackTrace(); 
              } 
            } 
          }).start(); 
        } 
      }

      感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)簡(jiǎn)單的文件下載與上傳”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!


      本文名稱:Android如何實(shí)現(xiàn)簡(jiǎn)單的文件下載與上傳
      分享路徑:http://www.ef60e0e.cn/article/gehoij.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        东光县| 兴文县| 古交市| 中卫市| 昂仁县| 修文县| 四子王旗| 渭南市| 彭阳县| 长岛县| 新竹县| 蒙山县| 古丈县| 汉寿县| 叶城县| 东乌| 滦南县| 五指山市| 景德镇市| 聂拉木县| 莒南县| 绥江县| 安义县| 民和| 黄石市| 灵丘县| 辰溪县| 巴楚县| 巫山县| 日照市| 习水县| 乌拉特后旗| 徐汇区| 平远县| 福贡县| 巢湖市| 磐安县| 安远县| 彩票| 湾仔区| 壶关县|