简体   繁体   English

如何从数据库中检索图像并将其显示在div上

[英]how to retrive image and display it on div from database

I am new to the blob data conversion of content. 我是内容Blob数据转换的新手。 I am reading the emails and saving into database with body type as BLOB , if we have image embed into email even those are saving into it. 我正在阅读电子邮件并将其保存为主体类型为BLOB的数据库,如果我们将图像嵌入到电子邮件中,即使那些已保存到其中。 but the problem is retrieving back and showing it to users. 但是问题是找回并显示给用户。 The image re-creation is not happening it says image path failed. 图像重新创建未发生,表示图像路径失败。

the src in image tag would be something like this. 图像标签中的src将是这样的。

cid:image001.png@01CF630F.005FA080

Please help with the suitable java code which 'll form image back and we can show into the division . 请帮助提供合适的Java代码,该代码将形成图像,我们可以将其显示为该部分。

thanks in advance. 提前致谢。

I have solved same problem, by making a sevlet that returns the byte[] of the image. 我已经通过创建返回图像的byte []的sevlet解决了相同的问题。 And call this servlet within the img src tag. 并在img src标记内调用此servlet。

Example : This method will give you byte[] for any file, and method is in Utilities class 示例: 此方法将为您提供任何文件的byte [],并且该方法在Utilities类中

public static byte[] getFileData ( String fileName ){
    File f = new File(fileName);
    byte data [] = new byte[ (int) f.length() ];

    try {
        FileInputStream fis = new FileInputStream(f);
        fis.read(data);
        fis.close();
        return data;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}**

and I have called this method from doGet of the servlet as following: 并且我已经从servlet的doGet调用了此方法,如下所示:

final String imageName = request.getParameter("imageName");    
byte[] imageData = Utilities.getFileData( ROOT_DIR + imageName );
response.getOutputStream().write(imageData);

In above code ROOT_DIR is also defined as C:\\Temp\\FormData\\Images\\ 在上面的代码中,ROOT_DIR也定义为C:\\ Temp \\ FormData \\ Images \\

My div is like this: 我的div是这样的:

<div>
<img src='http://localhost:9080/LoadImage?imageName=one.png' />
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM