簡體   English   中英

壓縮 pdf 文件和下載在 java 中提供損壞的文件

[英]Zipping pdf files and downloading is giving corrupted file in java

我已經為其他文件(如 .txt 和 excel 文件)嘗試了此代碼,它們都運行良好。 相同的代碼如果我用於 pdf 文件,它不會顯示任何錯誤,但是當下載文件時,zip 文件夾將被損壞。 pdf文件是否有單獨的讀寫過程?

                   //create byte buffer
                    byte[] buffer = new byte[1024];
                    ZipOutputStream zout = new 
                    ZipOutputStream(response.getOutputStream());
                    Files[] files = new File(location).listFiles();
                    for(File pdfFile: files)
                            {
                                   FileInputStream fin = new FileInputStream(pdfFile);
                                   zout.putNextEntry(new ZipEntry(pdfFile);
                                   int length;
                                   while((length = fin.read(buffer)) > 0)
                                    {
                                       zout.write(buffer, 0, length);
                                    }
                            zout.closeEntry();
                            fin.close();
                            }
                 zout.close();
                    
            

此問題現已解決。 在我創建 pdf 文件的前一個循環中,輸出流沒有關閉,因此導致了問題。

暫無
暫無

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

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