Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javaservlet/JSP将服务器中的PDF文档嵌入HTML页面_Java_Html_Jsp_Pdf_Servlets - Fatal编程技术网

使用Javaservlet/JSP将服务器中的PDF文档嵌入HTML页面

使用Javaservlet/JSP将服务器中的PDF文档嵌入HTML页面,java,html,jsp,pdf,servlets,Java,Html,Jsp,Pdf,Servlets,我目前能够将文件从服务器目录发送到HTML前端。但是,我目前只能下载这些文件,或者在页面上完全打开这些文件。我想做的是将它们嵌入到我的网页中 如果文件存储在web目录中,但使用嵌入标记,我可以很容易地做到这一点 目前,我正在做以下工作: Servlet文件: @MultipartConfig @WebServlet public class FileServlet extends HttpServlet { public void doPost(HttpServletRequest r

我目前能够将文件从服务器目录发送到HTML前端。但是,我目前只能下载这些文件,或者在页面上完全打开这些文件。我想做的是将它们嵌入到我的网页中

如果文件存储在web目录中,但使用嵌入标记,我可以很容易地做到这一点

目前,我正在做以下工作:

Servlet文件:

@MultipartConfig
@WebServlet
public class FileServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        File file = new File("filePath");
        String mimeType = getServletContext().getMimeType(file.getName());
        response.setHeader("Content-Type", mimeType);
        response.setHeader("Content-Length", String.valueOf(file.length()));
        response.setHeader("Content-Disposition", "embed; filename=\"fileName.pdf\""); 
        Files.copy(file.toPath(), response.getOutputStream());
     }
}
JSP:


我希望您已经尝试过使用嵌入标记来显示没有表单提交逻辑的文件了?是的,我已经尝试过了,但这不允许我从服务器访问文件;仅来自web目录。我不确定我是否完全清楚这一点,因此从您返回的响应来看,将有一个包含某些内容的页面+一个包含嵌入pdf的部分。我的理解正确吗?是的,这正是我想要的。但到目前为止,我希望嵌入页面某个部分的pdf要么全屏显示,要么下载。这是因为我们将响应mime类型发送回文件。您可以将响应作为文本发送,并将文件路径作为属性发送,该属性可用作在页面中嵌入文件的源路径。
<form action="getFile" method="post" id="fileForm">
  <input type="hidden" name="fileName" value="<%out.print(cvKey);%>/<%out.print(name);%>.pdf"/>
</form>
<iframe name="frame" width="500" height="300"></iframe>
<embed id="embeddedPDF" src="/home/user/resources/candidate.pdf" width="1000" height="1000" type='application/pdf'></embed>
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.