Service 的 RandomAccessFile 對象一直空 打斷點了也看不出來 命名new了啊 而且文件長度獲取一直是0
//初始化子線程
class InitThread extends Thread {
? ?private FileInfo mFileInfo;
? ?public InitThread(FileInfo mFileInfo) {
? ? ? ?this.mFileInfo = mFileInfo;
? ?}
? ?public void run() {
? ? ? ?HttpURLConnection connection = null;
? ? ? ?RandomAccessFile raf = null;
? ? ? ?try {
? ? ? ? ? ?//連接網(wǎng)絡文件
? ? ? ? ? ?URL url = new URL(mFileInfo.getUrl());
? ? ? ? ? ?connection = (HttpURLConnection) url.openConnection();
? ? ? ? ? ?connection.setConnectTimeout(3000);
? ? ? ? ? ?connection.setRequestMethod("GET");
? ? ? ? ? ?int length = -1;
? ? ? ? ? ?if (connection.getResponseCode() == 206) {
? ? ? ? ? ? ? ?//獲得文件長度
? ? ? ? ? ? ? ?length = connection.getContentLength();
? ? ? ? ? ?}
? ? ? ? ? ?if (length <= 0) {
? ? ? ? ? ? ? ?return;
? ? ? ? ? ?}
? ? ? ? ? ?File dir = new File(DOENLOAD_PATH);
? ? ? ? ? ?if (!dir.exists()){
? ? ? ? ? ? ? ?dir.mkdir();
? ? ? ? ? ?}
? ? ? ? ? ?//在本地創(chuàng)建文件
? ? ? ? ? ?File file = new File(dir , mFileInfo.getFileName());
? ? ? ? ? ?raf = new RandomAccessFile(file,"rwd");
? ? ? ? ? ?//設置文件長度
? ? ? ? ? ?raf.setLength(length);
? ? ? ? ? ?mFileInfo.setLength(length);
? ? ? ? ? ?handler.obtainMessage(MSG_INIT,mFileInfo).sendToTarget();
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}finally {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?connection.disconnect();
? ? ? ? ? ? ? ?raf.close();
? ? ? ? ? ?} catch (NullPointerException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ?}
? ?}
}
2019-04-13
可能是DownloadService中間的創(chuàng)建文件夾這一步不行
你需要手動創(chuàng)建這個文件夾,我是這樣解決? 這一句raf = new RandomAccessFile(file,"rwd"); 的找不到文件
2017-05-15
你的MainActivity傳過來的沒有文件名,可能是這樣