如何在servlet中使用inputstream显示图像? [英] How to display an image using inputstream in servlets?

查看:396
本文介绍了如何在servlet中使用inputstream显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串格式的图像的 InputStream 。如何使用servlet在浏览器中显示该图像?

I have a InputStream of an image in string format. How to display that image in the browser using servlets?

这是(字符串的开头)字符串。

This is the (start of the) string.

/9j/4AAQSkZJRgABAgAAAQABAAD/4QDVRXhpZgAASUkqAAgAAAAIABIBAwABAAAAAQAAABoBBQABAAAAbgAAABsBBQABAAAAdgAAACgBAwABAAAAAgAAADEBAgANAAAAfgAAADIBAgAUAAAAiwAAABMCAwABAAAAAQAAAGmHBAABAAAAnwAAAAAAAABkAAAAAQAAAGQAAAABAAAAQ...


推荐答案

您需要将图像作为字节数组写入响应的输出流。这样的事情:

You need write the image as a byte array to the response's output stream. Something like this:


byte [] imageBytes = getImageAsBytes();

响应。 setContentType(image / jpeg);

response.setContentLength(imageBytes.length);

response.getOutputStream()。write(imageBytes);

然后在JSP中你只需使用一个标准的img元素:

Then in you JSP you just use a standard img element:

<img src="url to your servlet">

资料来源: https://stackoverflow.com/a/1154279/1567585

这篇关于如何在servlet中使用inputstream显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆