下载文件可在PC上运行,但在Android上只能获得16kb

时间:2013-11-24 03:02:28

标签: java android url download

我一直试图这么做多年。执行时,确切的代码完全适用于直接Java,但在Android中使用时,它最多只能下载16kbs才能停止。我不知道为什么。

代码是:

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是正确的pdf大小小于16kb,但只是停在这个大小的所有pdf。就像我说的,在非Android环境中,它可以100%运行。

我也试过这个:

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();

这给了我大小超过16kb的文件,但这些只是空白。

失败的示例网址:

"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"

2 个答案:

答案 0 :(得分:1)

我想知道(没有测试)BufferedReader是否因为超时或其他问题而分配了8192字节的内部缓冲区并在第3轮上失败。以下代码从BufferedInputStream一次读取一个字节,并且在Android上适用于我。你可以尝试一下,看看它是否解决了这个问题?

    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();
    }

答案 1 :(得分:0)

您可以尝试以下代码来解决此问题:

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);
  }
 }