2015-03-31 15 views
0

我已經完成了使用spring MultipartFile將文件上傳到我的目標文件夾中的工作現在,我希望查看PDF中點擊查看按鈕的相同文件。在春天的PDF文件中查看文件內容(不可讀)MVC

這是我的jsp。

<form method="POST" action="fileSearch"> 

     <table id="abc1" cellpadding="9" bgcolor="74BAAC" width="50%" 
      style="margin-top: 8px; margin-left: 320px;" cellspacing="9"> 
      <tr> 

       <td>Name:</td> 
       <td><input type="text" name="name"></td> 
       <td>Category:</td> 
       <td><select name="seldCategory" id="selectFileCategory"> 
         <option value="selectFileCategory">selectFileCategory</option> 
         <option value="FileCategory1">FileCategory1</option> 
         <option value="FileCategory2">FileCategory2</option> 
         <option value="FileCategory3">FileCategory3</option> 
         <option value="FileCategory4">FileCategory4</option> 
       </select></td> 
       <td><input type="submit" value="search"></td> 
      </tr> 


     </table> 
     <c:if test='${not empty "${fileSearchList}"}'> 

      <table align="center" class="viewAllTripTable" border="1" 
       style="display: center"> 
       <c:if test='${not empty "${fileSearchList}"}'> 
        <tr> 
         <td colspan="1"><b>Name:</b></td> 
         <td colspan="1"><b>Category:</b></td> 
         <td colspan="1"><b>View File</b></td> 
        </tr> 
       </c:if> 
       <c:forEach items="${fileSearchList}" var="fsList"> 
        <tr> 
         <td><c:out value="${fsList.name}" /></td> 
         <td><c:out value="${fsList.category}" /></td> 
         <td><c:out value="${fsList.file_path}" /></td> 

         <td><a class="buy" href="view?file_path=${fsList.file_path}&file_name=${fsList.name}">view</a></td> 
        </tr> 
       </c:forEach> 

      </table> 

     </c:if> 
    </form> 

我在這裏傳遞兩個參數的文件路徑和文件名,我認爲有很多校正在下面method.kindly建議我這樣做以正確的方式。我正嘗試在瀏覽器選項卡中查看特定文件。

  @RequestMapping(value = { "/view" }, method = RequestMethod.GET) 
      public String viewFile(@RequestParam("file_path") String file_path, 
        @RequestParam("file_name") String fileName, 
        HttpServletResponse response, HttpServletRequest request) 
        throws IOException, DocumentException { 

       String filePath = file_path.replace("/", "\\"); 
       System.out.println(filePath); 
       FileInputStream is = null; 
       BufferedReader bfReader = null; 
       try { 
        File file = new File(filePath); 
        FileInputStream inputStream = new FileInputStream(file); 
        byte[] bytes = new byte[(int) file.length()]; 
        inputStream.read(bytes); 

        FileOutputStream out = new FileOutputStream(file); 
        out.write(bytes); 

        Document d = new Document(); 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        PdfWriter.getInstance(d, baos); 
        d.open(); 
        d.add(new Paragraph("Hello World, iText")); 
        d.close(); 
        response.setContentType("application/pdf"); 
        response.setHeader("Content-disposition", "attachment; filename=" 
          + fileName + ".pdf"); 
        response.setHeader("Cache-Control", 
          "must-revalidate, post-check=0, pre-check=0"); 
        response.setHeader("Expires", "0"); 
        response.setHeader("Pragma", "No-cache"); 
        response.setContentLength(baos.size()); 
        ServletOutputStream outn = response.getOutputStream(); 
        baos.writeTo(outn); 
        out.close(); 

       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("sasa" + filePath); 
       return "searchPage"; 
      } 

回答

0

您可以嘗試使用以下方法之一。使用AbstractITextPdfView構建PDF並返回它。有 是需要一些配置。您可以湊合上​​

所示的例子 或者你可以試試這個:

更改方法簽名返回ResponseEntity<byte[]>和 進行其他更改如下:

@RequestMapping(value = { "/view" }, method = RequestMethod.GET) 
    public ResponseEntity<byte[]> viewFile(@RequestParam("file_path") String file_path, 
       @RequestParam("file_name") String fileName, 
       HttpServletResponse response, HttpServletRequest request) 
       throws IOException, DocumentException { 

     byte[] fileContents = // retrieve the file contents 

     HttpHeaders httpHeaders = new HttpHeaders(); 
     httpHeaders.setContentType(MediaType.parseMediaType("application/pdf")); 
     String fileName= "yourReqdFileName.pdf"; 
     httpHeaders.setContentDispositionFormData(fileName, fileName); 
     httpHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0"); 
     ResponseEntity<byte[]> serverResponse = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK); 
     return serverResponse; 
    } 
+0

嗨普拉莫德通過建議你我用下面的代碼嘗試。 – user2099038 2015-04-02 07:55:19

+0

嗨Pramod我的要求是上傳掃描文件,並點擊查看按鈕下載相同。正如你所建議的我嘗試使用代碼u張貼。它顯示當我打開它時無法加載PDF文檔。 – user2099038 2015-04-02 08:02:28

+0

只需檢查您生成的文檔是否正確。生成後,嘗試將其保存在磁盤上,然後直接打開。 – 2015-04-02 08:21:26