1. 程式人生 > >android FileInputStream分段讀取檔案 斷點續傳

android FileInputStream分段讀取檔案 斷點續傳

 final Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        File file = new File(path);
                        FileInputStream is = null;
                        // 獲取檔案大小
                        long length = file.length();
                        // 建立一個數據來儲存檔案資料
                        byte[] fileData = null;
                        try {
                            is = new FileInputStream(file);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        int bytesRead=0;
                        // 讀取資料到byte陣列中
                        List<ByteArrayInputStream> temp = new ArrayList<>();
                        int len = 0;
                        fileData = new byte[1000*1000*2];
                        while((len=is.read(fileData))!=-1)
                        {
                            temp = new ArrayList<>();
                            ByteArrayInputStream byteArrayInputStream= new ByteArrayInputStream(fileData);
                            temp.add(byteArrayInputStream);
                            //上傳流檔案
                            RegisterControlService.submitVedioSon(
                                    SubVedioViewActivity.this, temp, fInfos,subIdx);
                            temp.clear();
                            byteArrayInputStream.close();
                            subIdx++;
                        }
                        if(is != null)//完成後關閉輸入流
                            is.close();
                    } catch (Exception ex) {
                        System.out.print(ex.toString()+"dujq");
                        String a = ex + "";
                    }
                    handler.post(callBack);
                }
            };