簡體   English   中英

我對 PDF 文件感到困惑

[英]I'm confused about PDF files

如果我的代碼以這種方式繞過 PDF 說它無效並且無法打開,但是如果我交換它們並且在 A 之前有 B ,它工作正常嗎? 為什么會這樣,我該怎么做才能讓它工作? TIA

    InputStream in = new BufferedInputStream(conn.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

//A
    String line = "";
                StringBuilder builder = new StringBuilder();
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

//B
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            File outputFile = new File(directory, "goo.pdf");
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outputFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            byte[] buffer = new byte[1024];
            int len1 = 0;//init length
            while (true) {
                try {
                    if (!((len1 = in.read(buffer)) != -1)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fos.write(buffer, 0, len1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

InputStream 只能讀取一次。

在“A”中,讀取流並將內容放入 StringBuilder。
在“B”中,讀取流(現在為空)並通過管道傳輸到文件。
通過首先使用 A,輸出文件將始終為空。

只需刪除 A ,因為它在這里對您沒有任何作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM