How to display an image using tag in JSP(如何在 JSP 中使用标签显示图像)
问题描述
这是在动作类中检索图像的代码.我试过了,但无法显示它我不知道图像处理.JSP中如何使用标签来显示图片?
Here is the code to retrieve image in action class. I have tried it, but not able to display it I don't know about the image processing. How should I use tags in JSP to display the image?
public String displayImage()
{
Connection con = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql,result="";
con=JdbcHelper.getConnection();
if(con!=null)
{
try
{
sql = "SELECT INPUT_FILE_BLOB FROM message_details where message_id=?";
preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, messageid);
resultSet = preparedStatement.executeQuery();
if(resultSet.next())
{
Blob image = resultSet.getBlob("INPUT_FILE_BLOB");
System.out.println("=============Image2
" +image);
int len1 = (int) image.length();
System.out.println("=============len1
" +len1);
byte [] rb1 = new byte[len1];
InputStream readImg1 = resultSet.getBinaryStream(1);
try {
int index1=readImg1.read(rb1, 0, len1);
System.out.println("index1"+index1);
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb1,0,len1);
response.getOutputStream().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch(SQLException e)
{
try
{
con.rollback();
}
catch (SQLException e1)
{
result = e1.getMessage();
e.printStackTrace();
}
result = e.getMessage();
e.printStackTrace();
}
finally
{
JdbcHelper.close(resultSet);
JdbcHelper.close(preparedStatement);
JdbcHelper.close(con);
}
}
else
result = Constants.SUCCESS;
}
推荐答案
从数据库中检索图像作为 Blob
或字节数组后,您将直接写入响应.在这种情况下,您不应该返回 SUCCESS 结果,您需要返回 NONE 结果.然后在 JSP 中,您可以使用 img
标记将图像作为单独的线程访问,该标记将调用您返回图像的操作.
After retrieving an image from the database as Blob
or byte array you are writing direct to response. In this case you should not return SUCCESS result, you need to return NONE result. Then in the JSP you could access an image as a separate thread using img
tag that will call your action that return an image.
另见我们如何显示动态或可以作为字节数组提供的静态图像,和如何在Struts2中显示图像.
这篇关于如何在 JSP 中使用标签显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JSP 中使用标签显示图像


- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01