InputStream returns 下载 PDF 时为空文件

InputStream returns empty file when downloading a PDF

所以我正在尝试使用 HttpURLConnection 下载 PDF 文件,我认为我已经正确地完成了输入和输出流的所有操作,但是当我打开下载的 PDF 文件时(使用 Android and/or ADB),或者只是通过 t运行 将其转移到 OS X 来检查它,它完全是空的,大小显示为 0 字节。

我尝试从中下载 PDF 的站点是:

http://www.pdf995.com/samples/pdf.pdf

这是我的代码

    public static void DownloadFile(final String fileURL, final File directory) {

    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {

                try {
                    FileOutputStream f = new FileOutputStream(directory);
                    URL u = new URL(fileURL);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    //c.setRequestProperty("Content-Type", "application/pdf");
                    //c.setDoOutput(true);
                    c.connect();
                    Log.d("debugz", Integer.toString(c.getContentLength()));

                    InputStream in = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = in.read(buffer)) > 0) {
                        f.write(buffer, 0, len1);
                    }
                    f.flush();
                    f.getFD().sync();
                    f.close();


                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();


}


public static String getPDF(String pdfurl) {

    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    File folder = new File(extStorageDirectory, "schematisktscheman");
    folder.mkdir();
    File file = new File(folder, "schemainfo.pdf");
    try {
        file.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    DownloadFile(pdfurl, file);

    return null; //added return
}

我主要activity:

SchedulePdfProcessor.getPDF("http://www.pdf995.com/samples/pdf.pdf");
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/schematisktscheman/schemainfo.pdf")));

编辑:我运行 主线程上的网络代码引发了异常。现在为下载创建一个新线程,它获取示例 PDF (http://www.pdf995.com/samples/pdf.pdf) 并将其内容放入文件中。 感谢@greenapps!

首先,替换:

f.close();

与:

f.flush();
f.getFD().sync();
f.close();

这可确保在继续之前将所有内容写入磁盘。

然后,您需要使用 MediaScannerConnectionscanFile() 来获取 MediaStore 以了解更新的文件。

我运行主线程上的网络代码,抛出了异常。现在创建一个用于下载的新线程,它获取示例 PDF (http://www.pdf995.com/samples/pdf.pdf) 并将其内容放入文件中。

Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {

                try {
                    FileOutputStream f = new FileOutputStream(directory);
                    URL u = new URL(fileURL);
                    c = (HttpURLConnection) u.openConnection();
                    c.setRequestMethod("GET");
                    //c.setRequestProperty("Content-Type", "application/pdf");
                    //c.setDoOutput(true);
                    c.connect();
                    Log.d("debugz", Integer.toString(c.getContentLength()));

                    InputStream in = c.getInputStream();

                    byte[] buffer = new byte[1024];
                    int len1 = 0;
                    while ((len1 = in.read(buffer)) > 0) {
                        f.write(buffer, 0, len1);
                    }
                    f.flush();
                    f.getFD().sync();
                    f.close();


                } catch (Exception e) {
                    e.printStackTrace();
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();