2013-11-24 1 views
0

나는 이것을 오래 전부터 사용해 왔습니다. 정확한 코드는 자바가 실행될 때 완벽하게 작동하지만 안드로이드에서 사용되는 경우 중지하기 전에 최대 16kbs 만 다운로드합니다. 나는 이유를 모른다.파일을 다운로드하는 것은 PC에서 작동하지만 Android에서는 16kb 만받을 수 있습니다.

코드는 다음과 같습니다

URL website = new URL(url); 
URLConnection connection = website.openConnection(); 
connection.connect(); 
int fileSize = connection.getContentLength(); 
ReadableByteChannel rbc = Channels.newChannel(website.openStream()); 
FileOutputStream fos = new FileOutputStream(Initializer.getPath(saveName)); 
fos.getChannel().transferFrom(rbc, 0, fileSize);//using filesize = very large number has no effect except eventual out of memory issues. 

위의 다운로드 PDF의 제대로 16킬로바이트보다 작은 크기의 PDF 파일의뿐만 단지 모든 PDF 파일의이 크기에서 멈 춥니 다. 그리고 내가 말했듯이, 비 Android 환경에서 이것은 100 % 작동합니다.

나는 또한이 시도 : 나에게 16킬로바이트보다 큰 크기의 파일을 제공하지만이 그냥 비어 있습니다

Reader reader = new InputStreamReader(connection.getInputStream()); 
      BufferedReader br = new BufferedReader(reader); 
      File f = new File(Initializer.getPath(saveName)); 
      BufferedWriter bw = new BufferedWriter(new FileWriter(f)); 

      String line = ""; 
      while ((line = br.readLine()) != null) 
      { 
       bw.write(line); 
       bw.newLine(); 
      } 
      br.close(); 
      bw.close(); 

합니다. (때문에 그것의 크기) 작동

"https://www.uct.ac.za/downloads/uct.ac.za/currentstud/services/jammie/term/10_hiddingh_weekwkndph.pdf" 

예 :

"https://www.uct.ac.za/downloads/uct.ac.za/currentstud/services/jammie/vac/10_hiddingh.pdf" 

답변

1

을, BufferedReader가의 내부 버퍼를 할당하는 경우, 내가 (테스트없이) 궁금하네요 실패

URL의 예 8192 바이트이며 시간 초과 또는 기타 문제로 인해 3 차회에서 실패합니다. 다음 코드는 BufferedInputStream에서 한 번에 한 바이트를 읽고 안드로이드에서 잘 작동합니다. 이 문제를 해결할 수 있는지 알려 줄 수 있습니까?

try { 
    //URL u = new URL("https://www.uct.ac.za/downloads/uct.ac.za/currentstud/services/jammie/term/10_hiddingh_weekwkndph.pdf"); 
    URL u = new URL("https://www.uct.ac.za/downloads/uct.ac.za/currentstud/services/jammie/vac/10_hiddingh.pdf"); 

    HttpsURLConnection connection; 

    connection = (HttpsURLConnection)u.openConnection(); 
    BufferedInputStream is = new BufferedInputStream(connection.getInputStream()); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    int byte_read = is.read(); 
    while(byte_read != -1) 
    { 
     bos.write(byte_read); 
     byte_read = is.read(); 
    } 

    byte[] bos_data = bos.toByteArray(); 
    System.out.println("buffer data length " + bos_data.length); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

. private fill() 메소드의 두 루프를 통해서만 만들고있다. 이유는 모르겠지만 시간 초과 일 수 있습니다. – mttdbrd

+0

안녕하세요 고마워요. 필자는 이것을 FileOutputStream을 사용하여 파일에 쓰려고 시도했다가 BufferedWrtier를 시도했습니다. 둘 다 결과적으로 크기가 매우 작은 파일 (0.3kb)을 제공했습니다. 타임 아웃이 문제라고 생각했는데, 이것을 어떻게 높게 설정해야하는지 알고 있습니까? – Mafro34

+0

방금 ​​Eclipse 프로젝트를 만들었고 테스트를하고 있습니다. 내가 찾은 것을 알려주지. – mttdbrd

0

이 문제를 해결하기 위해 코드를 아래에 시도 할 수 있습니다 : 그것은 단지 BufferedReader의 내부처럼 보이는 8192 바이트의 버퍼를 지 웁니다

public void DownloadFromUrl(String DownloadUrl, String fileName) { 
    try { 
     File root = android.os.Environment.getExternalStorageDirectory(); 
     File dir = new File (root.getAbsolutePath() + "/xmls"); 
     if(dir.exists()==false) { 
      dir.mkdirs(); 
     } 

     URL url = new URL(DownloadUrl); //you can write here any link 
     File file = new File(dir, fileName); 

     long startTime = System.currentTimeMillis(); 
     Log.d("DownloadManager", "download url:" + url); 

     /* Open a connection to that URL. */ 
     URLConnection ucon = url.openConnection(); 

     /* 
     * Define InputStreams to read from the URLConnection. 
     */ 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 

     /* 
     * Read bytes to the Buffer until there is nothing more to read(-1). 
     */ 
     ByteArrayBuffer baf = new ByteArrayBuffer(5000); 
     int current = 0; 
     while ((current = bis.read()) != -1) { 
      baf.append((byte) current); 
     } 

     /* Convert the Bytes read to a String. */ 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(baf.toByteArray()); 
     fos.flush(); 
     fos.close(); 
     Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
    } catch (IOException e) { 
    Log.d("DownloadManager", "Error: " + e); 
    } 
} 
관련 문제