最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)簡單的文件下載與上傳

 更新時間:2018年12月24日 10:08:11   作者:pigdreams  
今天小編就為大家分享一篇關(guān)于Android實(shí)現(xiàn)簡單的文件下載與上傳,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

文件下載

/**
 * 下載服務(wù) IntentService 
 * 生命周期:
 * 1>當(dāng)?shù)谝淮螁覫ntentService時,Android容器
 *  將會創(chuàng)建IntentService對象。
 * 2>IntentService將會在工作線程中輪循消息隊(duì)列,
 *  執(zhí)行每個消息對象中的業(yè)務(wù)邏輯。
 * 3>如果消息隊(duì)列中依然有消息,則繼續(xù)執(zhí)行,
 *  如果消息隊(duì)列中的消息已經(jīng)執(zhí)行完畢,
 *  IntentService將會自動銷毀,執(zhí)行onDestroy方法。
 */
public class DownloadService extends IntentService{
  private static final int NOTIFICATION_ID = 100;
  public DownloadService(){
    super("download");
  }
  public DownloadService(String name) {
    super(name);
  }
  /**
   * 該方法中的代碼將會在工作線程中執(zhí)行
   * 每當(dāng)調(diào)用startService啟動IntentService后,
   * IntentService將會把OnHandlerIntent中的
   * 業(yè)務(wù)邏輯放入消息隊(duì)列等待執(zhí)行。
   * 當(dāng)工作線程輪循到該消息對象時,將會
   * 執(zhí)行該方法。
   */
  protected void onHandleIntent(Intent intent) {
    //發(fā)送Http請求 執(zhí)行下載業(yè)務(wù)
    //1. 獲取音樂的路徑
    String url=intent.getStringExtra("url");
    String bit=intent.getStringExtra("bit");
    String title=intent.getStringExtra("title");
    //2. 構(gòu)建File對象,用于保存音樂文件
    //   /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請求,獲取InputStream
      InputStream is = HttpUtils.getInputStream(url);
      //4. 邊讀取邊保存到File對象中
      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)滾動消息
      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ù)庫添加一條綁定記錄 
            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(); 
  } 
} 

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Android開發(fā)之滑動數(shù)值選擇器NumberPicker用法示例

    Android開發(fā)之滑動數(shù)值選擇器NumberPicker用法示例

    這篇文章主要介紹了Android開發(fā)之滑動數(shù)值選擇器NumberPicker用法,結(jié)合實(shí)例形式分析了Android滑動數(shù)值選擇器NumberPicker的功能、相關(guān)函數(shù)、事件監(jiān)聽、界面布局等操作技巧,需要的朋友可以參考下
    2019-03-03
  • Jetpack?Compose?實(shí)現(xiàn)一個圖片選擇框架功能

    Jetpack?Compose?實(shí)現(xiàn)一個圖片選擇框架功能

    這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個圖片選擇框架,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 詳解Flutter掃碼識別二維碼內(nèi)容

    詳解Flutter掃碼識別二維碼內(nèi)容

    這篇文章主要介紹了Flutter掃碼識別二維碼內(nèi)容的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Android編程實(shí)現(xiàn)滑動開關(guān)組件功能【附源碼下載】

    Android編程實(shí)現(xiàn)滑動開關(guān)組件功能【附源碼下載】

    這篇文章主要介紹了Android編程實(shí)現(xiàn)滑動開關(guān)組件功能,結(jié)合實(shí)例形式詳細(xì)分析了Android滑動開關(guān)組件的簡單布局與功能實(shí)現(xiàn)技巧,并附帶完整實(shí)例源碼供讀者下載參考,需要的朋友可以參考下
    2018-01-01
  • Android自定義view Path 的高級用法之搜索按鈕動畫

    Android自定義view Path 的高級用法之搜索按鈕動畫

    這篇文章主要介紹了Android自定義view Path 的高級用法之搜索按鈕動畫,需要的朋友可以參考下
    2017-06-06
  • Android編程實(shí)現(xiàn)啟動界面的方法分析

    Android編程實(shí)現(xiàn)啟動界面的方法分析

    這篇文章主要介紹了Android編程實(shí)現(xiàn)啟動界面的方法,結(jié)合實(shí)例形式分析了Android啟動界面的實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下
    2017-03-03
  • Android自定義View實(shí)現(xiàn)隨機(jī)數(shù)驗(yàn)證碼

    Android自定義View實(shí)現(xiàn)隨機(jī)數(shù)驗(yàn)證碼

    這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)隨機(jī)數(shù)驗(yàn)證碼效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-06-06
  • Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案

    Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案

    運(yùn)行時變更就是設(shè)備在運(yùn)行時發(fā)生變化(例如屏幕旋轉(zhuǎn)、鍵盤可用性及語言)。發(fā)生這些變化,Android會重啟Activity,這時就需要保存activity的狀態(tài)及與activity相關(guān)的任務(wù),以便恢復(fù)activity的狀態(tài)。為此,google提供了三種解決方案,本文將對這三種方案進(jìn)行逐一介紹。
    2016-12-12
  • Android實(shí)現(xiàn)縮放動畫

    Android實(shí)現(xiàn)縮放動畫

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)縮放動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • android使用ItemDecoration給RecyclerView 添加水印

    android使用ItemDecoration給RecyclerView 添加水印

    本篇文章主要介紹了android使用ItemDecoration給RecyclerView 添加水印,介紹了自定義Drawable來完成水印圖片和使用ItemDecoration來布局水印,有興趣的可以了解一下。
    2017-02-02

最新評論

吉水县| 从江县| 凯里市| 辽宁省| 衡山县| 上思县| 阿勒泰市| 崇阳县| 临西县| 门头沟区| 海城市| 北宁市| 赤城县| 栾城县| 观塘区| 察雅县| 修文县| 永福县| 中卫市| 凤庆县| 泸溪县| 舒城县| 灵石县| 吉木萨尔县| 西林县| 衢州市| 新闻| 三台县| 安溪县| 布尔津县| 息烽县| 洪洞县| 江西省| 彭阳县| 建平县| 高州市| 凌源市| 泰和县| 汨罗市| 县级市| 合肥市|