將圖像寫入數據庫中的jsp

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

我正在嘗試將數據寫入數據庫中的jsp(保存為BLOB)。我正在使用spring並且在我的控制器中,我確實有圖像byte []。

So I am doing this

所以我這樣做

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

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

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

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

In my jsp, I have

在我的jsp中,我有

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

”alt =“my image”/>

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

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

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

K_&W:???= 5)^ - Øř滋* \ * M 1 ??] ?,Z?我?P 450ž〜v· K + LMS?如參見]一個?他/ ?; k]的W'C嗎?如參見] ?? ?????曲〜 - LZ Z:6 ?? Z = A + 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〜?vmZi q ULF%LZ〜vozi;?!?&FVϨķü޵ü?? VXXѯ? > W [çֻ的Kt ?????)在線2 b} M·G·hѯA/?濟( 3?

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

我甚至嘗試將byte []轉換為ByteArrayOutputStream並使用Base64對其進行編碼,但是沒有工作

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

但是當我使用FileOutputStream將byte []寫入文件(myimage.jpg)時,我確實看到了使用老式方式在我的jsp中顯示的圖像

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

5 个解决方案

#1


5  

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.

您需要使用Spring MVC Controller方法,它將您的圖像作為byte []寫入您的HttpServletResponse類。

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:

然后在客戶端上使用html代碼:

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

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

更新:是的,你可以從Spring 3.1開始使用@ResposneBody注釋

Register your ByteArrayHttpMessageConverter

注冊您的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;
}

#2


4  

The trick is to have another servlet that outputs the data directly to HttpServletResponse.getOutputStream(), and as SuKu says with the appropriate content type. Then in your JSP you just point to the URL your image servlet is mapped to in the <img/> tag. For instance, if the image servlet is mapped to /imgServlet, you would use something like this

訣竅是讓另一個servlet直接將數據輸出到HttpServletResponse.getOutputStream(),並且正如SuKu所說的那樣具有適當的內容類型。然后在JSP中,您只需指向圖像servlet在標記中映射到的URL。例如,如果圖像servlet映射到/ imgServlet,你可以使用這樣的東西

<img src="imgServlet?img=myimage" ... />

Try something like this http://www.avajava.com/tutorials/lessons/how-do-i-return-an-image-from-a-servlet-using-imageio.html

試試這樣的東西http://www.avajava.com/tutorials/lessons/how-do-i-return-an-image-from-a-servlet-using-imageio.html

#3


0  

Another option, if you like using the ModelAndView would be to create a custom View object that sets up the media type and outputs the bytes of the image to the output stream. This way you can use the ModelAndView to easily set redirects or return a normal error or even an JSP error page if there is an error, like image not found.

如果您喜歡使用ModelAndView,另一個選項是創建一個自定義View對象,該對象設置媒體類型並將圖像的字節輸出到輸出流。這樣,如果出現錯誤,您可以使用ModelAndView輕松設置重定向或返回正常錯誤甚至是JSP錯誤頁面,例如找不到圖像。

#4


0  

@RequestMapping(value="/imageDisplays/{imageId}")

public void getImage(@PathVariable int imageId,HttpSession session,HttpServletResponse response)  {
    OutputStream oImage;
    try{
            byte[] imageInByte = // get image in byte array
            response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
            oImage=response.getOutputStream();
            oImage.write(imageInByte);
            oImage.flush();
            oImage.close(); 
    }catch(Exception e){
        LOGGER.debug("Request could not be completed at this moment. Please try again.");
        e.printStackTrace();
    }
    }

#5


0  

This is a similar question to the one I was working on. However, I was using long poll instead of servlet. Here is the link stackoverflow.

這與我正在研究的問題類似。但是,我使用長輪詢而不是servlet。這是鏈接stackoverflow。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/02/15/76420d2e66b334f6edb4d140e28aba9e.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com