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 中使用标签显示图像
基础教程推荐
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
