简体   繁体   English

如何显示来自servlet的带有图像的html文件?

[英]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.

相关问题 如何从Java Servlet发送带有变量的HTML电子邮件? - How do I send an HTML email with variables from a java servlet? 如何在servlet中显示数据库中的图像? - how to display image from database in servlet? 包含来自上下文不同的文件当HTML文件位于不同的servlet上下文中时,如何在JSP文件中包含HTML文件? - Include files from context differents How do I include an HTML file in my JSP file when the HTML file is located within a different servlet context? 如何使用 eclipse 中的 servlet 将图像从 html 页面文件上传并从数据库获取图像到 html 页面中的数据库中 - How to insert image into databse from html page file upload and fetch image from databse to html page, using servlet in eclipse 如何通过从文件资源管理器中选择一个文件来使用javaFX显示图像 - How do I display an image using javaFX by selecting a file from file explorer 从HTML获取图像/视频作为新File()到Java Servlet - Getting image/video from html into java servlet as new File() 如何使用java servlet从DB在jsp上显示图像 - How to display image on jsp from DB using java servlet 如何使用Java Servlet从JSP中的数据库显示图像? - How to display an image from a database in a JSP using a java servlet? 如何创建从Netbeans中的Java servlet到index.html或其他页面的链接? - How Do i create a link from a Java servlet in Netbeans back to a index.html or another page? 我如何知道是否从Servlet点击了我的html链接? - how do i know whether my html link is clicked or not from Servlet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM