[英]How do I display a html file with an image, from a servlet?
I want to display a html file from my servlet, the html file is stored in a different server than my webapp, I'm currently using the FileInputStream and ServletOutputStream classes to make it work, the problem is that it doesn't display the image. 我想显示来自Servlet的html文件,该html文件存储在与Webapp不同的服务器中,我目前正在使用FileInputStream和ServletOutputStream类使其工作,问题是它不显示图像。 This is the code of my servlet: 这是我的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);
}}
And this is my html file: 这是我的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>
As a result, it does displays the html page, but the image is lost, somehow. 结果,它确实显示了html页面,但是图像以某种方式丢失了。 I think that maybe is because the setContentType in which I specify text and I'm sending also an image? 我认为这可能是因为我在其中指定文本并且还要发送图像的setContentType吗? Do you have any suggestions? 你有什么建议吗? Also, the part using the getRequestDispatcher, gets me in trouble because it won't accept a specific path, and I don't think I will be putting the files in the application folder. 另外,使用getRequestDispatcher的部件也给我带来麻烦,因为它不接受特定的路径,而且我认为我不会将文件放在应用程序文件夹中。 Thank you for your help. 谢谢您的帮助。
Problem is since you don't have the files in your web app folder. 问题是因为您的Web应用程序文件夹中没有文件。 The html generated still points to image at location file:\\\\C:\\\\Sistemas\\\\tendBe.png
at server, which is certainly not accessible for the client. 生成的html仍然指向服务器上位置file:\\\\C:\\\\Sistemas\\\\tendBe.png
上的图像,客户端肯定无法访问该图像。
As of now my suggestion is to create another servlet to read the image like you did for the html. 到目前为止,我的建议是创建另一个servlet来像读取html一样读取图像。 Also update the image src in the html to the new image servlet url. 还将html中的图像src更新为新的图像servlet URL。
Or you may convert the image to base64 string, data url
and use it as the image src in the html page. 或者,您可以将图像转换为base64字符串, data url
并将其用作html页面中的图像src。 By this you can skip the need of creating new image servlet. 这样,您可以跳过创建新图像servlet的需要。 There are several online tools to convert image like . 有几个在线工具,图像转换一样 。
Using data url <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
You have to change the src
attribute of the img
tags in your HTML, to provide a proper URL of the images. 您必须在HTML中更改img
标签的src
属性,以提供正确的图像URL。 And you have to find a way to serve these images from the server. 而且,您必须找到一种从服务器提供这些图像的方法。
Hint: Images are (usually) served with a separate request, ie browser makes a request to fetch the HTML, then analyses the HTML, and if it finds elements, that need to be downloaded (such as images, or CSS/Javascript files), the browser makes a new request to fetch them (one request per element). 提示:(通常)为图像提供单独的请求,即浏览器发出获取HTML的请求,然后分析HTML,如果找到了需要下载的元素(例如图像或CSS / Javascript文件) ,浏览器会发出一个新请求来提取它们(每个元素一个请求)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.