2011-09-13 36 views
5

Tôi đang tạo ứng dụng Android và tôi cần tải xuống tệp từ url, lớn 33 MB.Android: "Ngoại lệ không mong muốn" ngoại lệ tải xuống các tệp lớn

Đây là nhiệm vụ download:

try { 
      int MAX_BUFFER_SIZE = 4096; 
      URL mUrl = new URL(params[0]); 
      HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); 
      connection.setRequestMethod("GET"); 
      long length = connection.getContentLength(), downloaded = 0; 
      int read; 
      byte [] buffer = new byte[(((int)length) > MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE : (int)length]; 
      String filename = getFilename(mUrl); 
      File file = new File (SDCARD_ROOT); 
      if (!file.exists() || !file.isDirectory()){ 
       file.mkdir(); 
      } 
      this.filename = filename; 
      file = new File (SDCARD_ROOT + this.filename); 
      FileOutputStream fos = new FileOutputStream (file); 
      //Start downloading 
      InputStream stream = connection.getInputStream(); 

      while ((read=stream.read(buffer)) > -1){ 
       fos.write(buffer, 0, read); 
       downloaded += read; 
       publishProgress((int) ((float) downloaded/length * 100)); 
      } 
      fos.close(); 
      return 1; 
     } catch (Exception e){ 
      Log.e("REV-PARTS", "Revolver parts error in DownloadTask: " + e.getMessage()); 
      return 2; 
     } 

Nó hoạt động đúng với các file nhỏ (1-15 mb), nhưng nó sẽ trả về một "kết thúc bất ngờ của dòng" ngoại lệ với các tập tin lớn. Cảm ơn trước.

+0

Cuộc gọi nào ném ngoại lệ? 'stream.read()'? –

+1

Yep, stream.read – Gnufabio

+0

@Gnufabio bạn có thể khắc phục sự cố này không? –

Trả lời

-2

khi bạn bắt ngoại lệ, tôi cố gắng phương pháp downContinue(), tôi có thể hiển thị mã của tôi: Phương pháp

private void downloadApk() { 
    thread1 = new Thread() { 
     public void run() { 
      File oFile = null; 
      try { 
       URL url = new URL(PQGLApplication.resrootURL + "apk/PQGLMap.apk"); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       ReadableByteChannel channel = 
         Channels.newChannel(urlConnection.getInputStream()); 
       oFile = 
         new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
           + "/" + "hy_ht_new/" + "test2" + ".apk"); 
       oFile.setWritable(true); 
       oFile.setReadable(true); 
       if (oFile.exists()) { 
        oFile.delete(); 
       } 
       FileOutputStream fos = new FileOutputStream(oFile); 
       fileSize = urlConnection.getContentLength(); 
       ByteBuffer buffer = ByteBuffer.allocate(1024); 
       int noOfBytes = 0; 
       byte[] data = null; 
       sendApkMessage(0, 0); 
       while ((noOfBytes = channel.read(buffer)) > 0) { 
        data = new byte[noOfBytes]; 
        System.arraycopy(buffer.array(), 0, data, 0, noOfBytes); 
        buffer.clear(); 
        fos.write(data, 0, noOfBytes); 
        downLoadFileSize += noOfBytes; 
        sendApkMessage(1, downLoadFileSize); 
       } 
       fos.flush(); 
       fos.close(); 
       channel.close(); 
       sendApkMessage(2, oFile.getAbsolutePath()); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       downContinue(); 
      } 
     }; 
    }; 
    thread1.start(); 
} 

private void downContinue() { 
    continueTime++; 
    try { 
     if (continueTime == 3) { 
      continueTime = 0; 
      sendApkMessage(4, 0); 
      Log.e("what is the fucking continuetime", "continueTime" + continueTime); 
     } else { 
      URL url = new URL(PQGLApplication.resrootURL + "apk/PQGLMap.apk"); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
      File oFile = 
        new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" 
          + "hy_ht_new/" + "test2" + ".apk"); 
      RandomAccessFile oSavedFile = new RandomAccessFile(oFile, "rw"); 
      FileOutputStream fos = new FileOutputStream(oFile); 
      ReadableByteChannel channel = Channels.newChannel(urlConnection.getInputStream()); 
      // oSavedFile.seek(nPos); 
      ByteBuffer buffer = ByteBuffer.allocate(1024); 
      byte[] data = null; 
      int temp = 0; 
      sendApkMessage(3, oFile.getAbsolutePath()); 
      while ((temp = channel.read(buffer)) > 0) { 
       data = new byte[temp]; 
       System.arraycopy(buffer.array(), 0, data, 0, temp); 
       buffer.clear(); 
       fos.write(data, 0, temp); 
      } 
      fos.flush(); 
      fos.close(); 
      oSavedFile.close(); 
      sendApkMessage(2, oFile.getAbsolutePath()); 
      continueTime = 0; 
     } 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Log.e("what is the fucking exception", e.toString() + continueTime); 
     downContinue(); 
    } 

} 

downContinue này được sử dụng để giải quyết problem.At này ít nhất, các tập tin được tải về thành công! Tôi đến từ Trung Quốc. Tiếng Anh của tôi không tốt lắm.

+0

bạn có thể giải thích phương thức sendApkMessage(), chính xác nó đang làm gì? – skygeek

+0

Bạn không cần đặt tập tin đầu ra có thể đọc được hoặc có thể ghi hoặc xóa trước nếu nó tồn tại. 'FileOutputStream()' mới đã có tất cả các hiệu ứng đó. Bạn không cần 'System.arraycopy()'. Phương thức dựa vào mã ngoài không xác định. – EJP

-1

Đối với các tệp lớn, bạn cần đặt thời gian kết nối theo cách thủ công bằng cách sử dụng mã sau. Tôi đã đặt thời gian tối đa 3 phút

connection.setConnectTimeout(180000); 
    connection.setReadTimeout(180000); 
+0

Làm cách nào để giải quyết vấn đề 'kết thúc luồng không mong muốn'? – EJP

+0

nó sẽ xảy ra khi lỗi này xuất hiện trong quá trình tải xuống do hết thời gian chờ, –

0

tôi có câu trả lời cho điều này. bạn nên thêm tiêu đề vào kết nối của mình.

connection.setRequestProperty("Accept-Encoding", "identity"); 

này sẽ giúp you.say tôi nếu nó giúp ...

0

Thiết lập một kích thước đoạn dường như làm việc cho tôi.

connection.setChunkedStreamingMode(1048576); 
Các vấn đề liên quan