Я закончил загрузку файлов в папку-получатель с помощью 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";
}
Привет Pramod как это было предложено вы меня попробовали с приведенным ниже кодом. – user2099038
Hi Pramod мое требование - загрузить отсканированные документы и загрузить их же при нажатии кнопки просмотра. Как вы предлагали, я пробовал с кодом u отправил. Он показывает Не удалось загрузить pdf doccument, когда я его открываю. – user2099038
Проверьте, правильно ли вы создаете документ. После генерации попробуйте сохранить его на диске, а затем открыть напрямую. –