在Java Web应用程序中,我必须在客户端机器中打开PDF文件,该文件也可以共享,而无需复制到客户端。它应该在客户端的adobe阅读器中打开。
如果文件位于客户端,如果他双击该文件,它将在Adobe阅读器中打开:-)
但是服务器无法打开位于客户端的文件。如果需要打开web应用程序,则文件需要位于服务器可读位置。您可以编写一个servlet来打开PDF文件。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream servletOutputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
// Public user guide
String fileName = "MyFile.pdf";
bufferedInputStream = new BufferedInputStream(new FileInputStream(fileName));
byte[] data = new byte[bufferedInputStream.available()];
bufferedInputStream.read(data);
response.setContentType("application/pdf");
servletOutputStream = response.getOutputStream();
bufferedOutputStream = new BufferedOutputStream(servletOutputStream);
bufferedOutputStream.write(data);
} catch (Exception e) {
LOG.error(e);
} finally {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
if (servletOutputStream != null) {
servletOutputStream.flush();
servletOutputStream.close();
}
}