【发布时间】:2015-09-16 17:19:36
【问题描述】:
我想从我的 servlet 显示一个 html 文件,该 html 文件存储在与我的 webapp 不同的服务器中,我目前正在使用 FileInputStream 和 ServletOutputStream 类使其工作,问题是它没有显示图像。 这是我的 servlet 的代码:
public class DisplayImage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
ServletOutputStream out;
out = response.getOutputStream();
FileInputStream fin = new FileInputStream("C:\\Sistemas\\tendBe.html");
BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch = 0;;
while ((ch = bin.read()) != -1) {
bout.write(ch);
}
bin.close();
fin.close();
bout.close();
out.close();
//This doesn't work, it doesn't take that path.
// RequestDispatcher view = request.getRequestDispatcher("file://C://Sistemas//tendBe.html");
//view.forward(request, response);
}}
这是我的 html 文件:
<html>
<head>
<title>Grafico</title>
</head>
<body>
<h2 style='background-color:Lightgray;'><center>Tendencia Beetle</center> </h2>
<div align='center'><b>Fecha: 25/06/2015<br>
Status: 21:25</b></div>
<center><b> 75</b></div><center>
<br>
<Left><img src='tendBe.png'><Left>
<Left><img src='file:\\C:\\Sistemas\\tendBe.png'><Left>
</body>
</html>
结果,它确实显示了 html 页面,但不知何故,图像丢失了。我认为这可能是因为我在其中指定文本并且还发送图像的 setContentType? 你有什么建议吗? 此外,使用 getRequestDispatcher 的部分给我带来了麻烦,因为它不接受特定路径,而且我认为我不会将文件放在应用程序文件夹中。感谢您的帮助。
【问题讨论】: