Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing image to a jsp from database

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[].

So I am doing this

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

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

In my jsp, I have

<img src=data:image/jpg;base64,"<c:out value='${myimage}'/>" alt="my image" />

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

���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�?����

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

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

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" .... />

like image 620
mi3 Avatar asked Feb 15 '13 16:02

mi3


People also ask

How can we save data from database in JSP?

For insert data in MySQL using JSP first we have to create a table in data base. The INSERT INTO statement is used to insert new data to a MySQL table: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) To learn more about SQL, please visit our SQL tutorial.

How can we edit data from database in JSP?

Step 1) Add an Edit button or link in the userList. jsp with a query string that contains the id of the selected user. And send the request to RegistrationCTL. Step 2) In RegistrationCTL, Under the doGet method: get id as request and get data from the database by the Id.


1 Answers

Unfortunately it won't work.

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

example:

@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();
}

and then use html code on client:

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

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

Register your 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>

And then use yout controller:

@RequestMapping("/getPhoto/{id}")
public @ResponseBody byte[] getPhoto(@PathVariable("id") final String id) throws IOException {
    byte[] imageBytes = dao.getImage(id);
    return imageBytes;
}
like image 100
danny.lesnik Avatar answered Oct 24 '22 10:10

danny.lesnik