日期:2014-05-16 浏览次数:20661 次
private byte img[];
ClientUser user = new ClientUser(); user=UserService.getUserByUserId(sqlSession,user); request.getSession().setAttribute("LoginUserImg", user.getImg().length); //在这里需要用byte[]对象的length属性判断是否存放了图片,如果length为0,那么就没有图片,反之则有。
<c:if test="${LoginUserImg==0 }"> <img src="${pageContext.request.contextPath }/images/head.jpg" alt=" " width="100" height="118" align="middle" /> </c:if> <c:if test="${LoginUserImg!=0 }"> <img src="userImg.jsp?id=${id}" alt="${id}" width="100" height="118" align="middle"> </c:if>
<%@page contentType="image/jpeg; charset=utf8"%> <%@page import="java.io.OutputStream"%> <% String id = request.getParameter("id"); Boolean ret = true; id = id == null ? "" : id.trim(); userId = userId == null ? "" : userId.trim(); OutputStream os = response.getOutputStream(); if (!id.isEmpty()) { ret =UserService.getUserImgById( id, os); } os.flush(); os.close(); os = null; response.flushBuffer();//下面这三句是一定要加上的,否则就会报错。 out.clear(); out = pageContext.pushBody(); %>
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; public class DefaultImage { public static int getUserImg(OutputStream out, String path) { int size = -1; FileInputStream is = null; try { is = new FileInputStream(path); // size = is.available(); // 得到文件大小 byte[] bytes = new byte[1024]; int len = 0; // 开始读取图片信息 while (-1 != (len = is.read(bytes))) { out.write(bytes, 0, len); size += len; } out.flush(); out.close(); is.close(); is = null; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return size; } }
<c:if test="${LoginUserImg==0 }"> <img src="${pageContext.request.contextPath }/images/head.jpg" alt=" " width="100" height="118" align="middle" /> </c:if> <c:if test="${LoginUserImg!=0 }"> <img src="userImg.jsp?id=${id}" alt="${id}" width="100" height="118" align="middle"> </c:if>
<%@page contentType="image/jpeg; charset=utf8"%> <%@page import="java.io.OutputStream"%> <%@page import="java.io.FileInputStream"%> <%@page import="com.tools.DefaultImage"%> <% String id = request.getParameter("id"); String userId = request.getParameter("uid"); int imgLen = 0; id = id == null ? "" : id.trim();/**注意这个地方必须判断一下**/ userId = userId == null ? "" : userId.trim(); OutputStream os = response.getOutputStream(); if (!id.isEmpty()) { imgLen = UserService.getUserImgById( id, os);//Get Validate Information } else if (!userId.isEmpty()) { imgLen =UserService .getUserImgByUserId(userId, os); } //开始读取默认图片 [color=red]String path = request.getRealPath("/images/head.jpg");[/color] //获取项目下的相对路径 if (imgLen <= 0) { imgLen = DefaultImage.getUserImg(os, path); } else { os.flush(); os.close(); } os = null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); %&g