Jsp uses a stream to read images from the database. If any image is displayed, the default image is displayed.

Source: Internet
Author: User

[Size = medium] The Avatar stored in the database is byte [] bytes. The image is read from the database using a stream based on the user id on the jsp page. However, if the user does not have an Avatar, it will be displayed on the page as a small box of pictures, which is ugly. Therefore, if the user does not have an avatar, the user will be given a default avatar.
In the User class:
Java code
1. private byte img [];

In the UserAction class:
Java code
1. ClientUser user = new ClientUser ();
2. user = UserService. getUserByUserId (sqlSession, user); request. getSession (). setAttribute ("LoginUserImg", user. getImg (). length );
3. // here, you need to use the length attribute of the byte [] object to determine whether the image is stored. If the length is 0, there will be no image; otherwise, there will be.

On the jsp page:
Java code
1. <c: if test = "$ {LoginUserImg = 0}">
2. 3. alt = "" width = "100" height = "118" align = "middle"/>
4. </c: if>
5. <c: if test = "$ {LoginUserImg! = 0} ">
6. 7. src = "userImg. jsp? Id =$ {id }"
8. alt = "$ {id }"
9. width = "100" height = "118" align = "middle">
10. </c: if>

UserImg. jsp:
Java code
1. <% @ page contentType = "image/jpeg; charset = utf8" %>
2. <% @ page import = "java. io. OutputStream" %>
3. <%
4. String id = request. getParameter ("id ");
5. Boolean ret = true;
6. id = null? "": Id. trim ();
7. userId = null? "": UserId. trim ();
8. OutputStream OS = response. getOutputStream ();
9. if (! Id. isEmpty ()){
10.
11. ret = UserService. getUserImgById (
12. id, OS );
13 .}
14. OS. flush ();
15. OS. close ();
16. OS = null;
17. response. flushBuffer (); // The following three sentences must be added; otherwise, an error is returned.
18. out. clear ();
19. out = pageContext. pushBody ();
20. %>

 

From 0609 xiaohua

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.