如何在<iframe>?</iframe>中显示本地磁盘文件系统的index.html文件

时间:2012-05-15 04:40:27

标签: jsp servlets struts2

我在index.html上的本地磁盘文件系统中有一个c:\report\index.html文件。我需要在<iframe>

中在Web应用程序的JSP页面中显示此文件

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

不是很简单吗?

<IFRAME SRC="D:\\lib\\hello.html" width="400" height="200">
<!-- Alternate content for non-supporting browsers -->
</IFRAME>

答案 1 :(得分:0)

创建一个servlet,在InputStream的帮助下获取FileInputStream,并在设置了正确的内容类型标题后将其写入响应的OutputStream(以便浏览器了解如何处理它)。最后,只需在<iframe src>中指定servlet的URL。

E.g。

@WebServlet("/reportServlet")
public class ReportServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        Inputstream input = new FileInputStream("c:/report/index.html");
        OutputStream output = response.getOutputStream();
        // Write input to output the usual way.
    }

}

<iframe src="${pageContext.request.contextPath}/reportServlet" ...></iframe>

另一种方法是将c:/report映射为服务器配置中的新webapp上下文,以便您可以通过http://example.com/report/index.html直接访问它。

<iframe src="/report/index.html" ...></iframe>
相关问题