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 中使用标签显示图像
基础教程推荐
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 从 python 访问 JVM 2022-01-01
