[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