在SpringMVC中查看pdf文件中的文件内容(不可读)
我已经完成了使用spring MultipartFile将文件上传到我的目标文件夹的工作,现在我希望在单击查看按钮时以pdf格式查看相同的文件 这是我的jsp在SpringMVC中查看pdf文件中的文件内容(不可读),spring,spring-mvc,Spring,Spring Mvc,我已经完成了使用spring MultipartFile将文件上传到我的目标文件夹的工作,现在我希望在单击查看按钮时以pdf格式查看相同的文件 这是我的jsp <form method="POST" action="fileSearch"> <table id="abc1" cellpadding="9" bgcolor="74BAAC" width="50%" style="margin-top: 8px; margin-left:
<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>
您可以尝试使用以下方法之一 使用AbstractITextPdfView构建PDF并返回它。那里 是否需要一些配置。你可以根据这个例子即兴创作 显示在 或者您可以尝试以下方法: 将方法签名更改为返回ResponseEntity和 进行以下其他更改:
@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;
}
Hi-Pramod根据您的建议,我尝试使用以下代码。Hi-Pramod我的要求是上传扫描的文档,并在单击查看按钮时下载相同的文档。按照你的建议,我试着用你发布的代码。当我打开pdf文档时,它显示无法加载它。请检查您生成的文档是否正确。生成后,尝试将其保存在磁盘上,然后直接打开。是的,现在它可以工作了。但是pdf文件不会自动打开。提前感谢。java.lang.IllegalArgumentException:org.springframework.util.Assert.notNullAssert.java:112上的未知返回值类型[[B]
@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;
}