将图像写入数据库中的jsp [英] writing image to a jsp from database

查看:88
本文介绍了将图像写入数据库中的jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据写入数据库中的jsp(保存为BLOB)。
我使用spring并且在我的控制器中,我确实有图像byte []。

I am trying to write an image to a jsp from database (saved as BLOB). I am using spring and in my controller, I do have the image byte[].

所以我这样做

byte [] imageBytes = dao.getImage( cc);

model.setAttribute(myimage,new String(imageBytes));

在我的jsp中,我有

< img src = data:image / jpg; base64,< c:out value ='$ {myimage}'/> alt =我的形象/>

但我只在我的jsp页面中看到ascii charcaters(如下所示)。

But I only see ascii charcaters in my jsp page (like below).

K _& w: =5 )^ - O? R ? z i* \ * * M,α1≠]?,Z'我?P 450ž〜v· K + LMS ?如参见]?一?他/ ?; K] w ^?C?è .Q] ??曲〜 - LZ Z:???????6 Z =一个+ E 5 ???? C. | WVý - ?? U·Λd克? ݭ)?A 10 7 $ ????] ??] S 3 BO?Lezhgzn? ? ?E . ?. ] < eOO ?S ?? . ] ? '? ? ?E ?` ] ֻD ??? \\ \\?}} U}? > T m z h t U| E}?K > T | Q]的 Vd的Q + GEA ?˰* WZI(SH∂ U ^ b - Z〜? ?vmZiq ULF%LZ〜vozi;?!&放大器;?FVϨķü޵ü ?VXX ??ѯ。> W [çֻ的Kt ?????)在线2 b} M·G·hѯA/? J e ( 3 ?

���K�_&�w:��=5�)^-����O?���R��?�z�i*\�*M�?��1�?�?�]?,��Z�?�I?�P??��?�z�~?v�?�k��?l�M�s�����?E���.��Q��]��?����a?h���e�/?�;�k�]����W�?c�?E���.��Q��]��??麯?~��-�?L��z?�Z�:?6??�z�=��a?��+���e�'�5�����??��?�?C���.�|��w�v?y��-�??U�?��?�D���?�g���ݭ)?A?�? 7��$��??�?�?�]??.���]�S�?�����bO��?L��e��z�h��gzn��?�?�?E���.�?.���]�<�eOO�?S��??� �˰.���]���?�ʿ?��?�?E��?`�]�ֻD��???�\?}U}?�>�T��m��z�h�t����U|E}?K��>�T� |�Q��]���Vd?�Q?�G��E�A�?�˰*�wz�i(sh?�U^�b?�z�~?v�m��Z�i�q?ULf%�L�z�~?v�o�z�i�;!&F�VϨ��?����K�?�u޵�u?��Vxx?�?ѯ��.�>W�[cֻKt��???�����??)e?b�}M�?���g�?h��ѯA/?��J��e�(����3�?����

我甚至尝试将byte []转换为ByteArrayOutputStream并使用Base64对其进行编码,但没有工作

I even tried to convert the byte[] to ByteArrayOutputStream and encode it with Base64, but didn;t work

model.addAttribute(image,Base64.encode(imageBytes));

但是当我使用FileOutputStream将byte []写入文件(myimage.jpg)时,我确实看到了显示的图像我的jsp使用老式的方式

But when I write the byte[] to a file(myimage.jpg) using FileOutputStream, I do see the image displayed in my jsp using the old fashioned way

< img src =../ images / myimage.jpg.... />

推荐答案

不幸的是它不起作用。

您需要使用Spring MVC Controller方法,它将您的图像作为byte []写入您的HttpServletResponse类。

You need to use Spring MVC Controller method which will write your your image as byte[] to your HttpServletResponse class.

示例:

@RequestMapping("/getImage/{id}")
public void getImage(HttpServletResponse response,@PathVariable("id") final String id) throws IOException {
    response.setContentType("image/jpeg");
    byte[] imageBytes = dao.getImage(id);
    response.getOutputStream().write(imageBytes);
    response.getOutputStream().flush();
}

然后在客户端使用html代码:

and then use html code on client:

<img src="getImage/222" ... />

更新:是的,您可以使用 @ResposneBody 从Spring 3.1开始的注释

Update: Yes you can do it with @ResposneBody annotation starting from Spring 3.1

注册 ByteArrayHttpMessageConverter

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>image/jpeg</value>
                    <value>image/png</value>
                </list>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

然后使用你的控制器:

@RequestMapping("/getPhoto/{id}")
public @ResponseBody byte[] getPhoto(@PathVariable("id") final String id) throws IOException {
    byte[] imageBytes = dao.getImage(id);
    return imageBytes;
}

这篇关于将图像写入数据库中的jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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