20141218_輸出PDF錯誤之byte[]

 

問題描述:當瀏覽器端使用Active控件如(AIP、iwebPDF)請求打開後臺PDF文件時,Java後臺處理請求的方法處理如下:

 

response.setCharacterEncoding("UTF-8");

      String filePath = request.getParameter("fileName");

      File file = new File(filePath);  

      try {

         if (!file.exists()) {         

            response.sendError(404, "文件沒有找到!");         

            return null;

         }  

         FileInputStream in = new FileInputStream(file);

         OutputStream out = response.getOutputStream();

         byte[] b = new byte[1024 * 8];

         while ((in.read(b)) != -1) {

            out.write(b);

         }

         out.flush();

         in.close();

         out.close();

      } catch (IOException e) {

         e.printStackTrace();

      }    

      return null;

前臺傳了文件在服務器的全路徑,存數據庫也是存全路徑。

紅色字體行,從內存申請8K字節內存作爲緩存,把文件流轉成文件流響應輸出到客戶端,問題很容易出現PDF緩衝到客戶端不完整,表現爲顯示有問題,另存爲處理後的文件異常,打開關閉後提示“是否保存修改…”

 

解決辦法是把8K改成1K,即1024問題就沒在出現。

 

原因分析:還沒找到!!!

 

 

其他解決方案:

int len = 0; // 字節長度

while((len = in.read(b))>0){ // 將讀取到的長度記錄下來

  out.write(b,0,len); // 讀取了多長的字節就寫入多長的字節

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章