其他分享
首页 > 其他分享> > 云笔记的项目分析(4):个人中心,加载头像

云笔记的项目分析(4):个人中心,加载头像

作者:互联网

<div class="col-md-4"><img style="width:240px;height:200px" src="user?action=userHead&imageName=${user.head }"></div>
else if("userCenter".equals(action)){
			//进入个人中心
			userCenter(request,response);
			
		}else if("userHead".equals(action)){
			//加载头像
			userHead(request,response);
		}
	private void userCenter(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//设置动态页面
		request.setAttribute("changePage", "user/info.jsp");
		//请求转发到首页
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
	}

index页面中设置了动态包含,设置changePage,request的setAttribute的里面,user/info.jsp

<c:if test="${!empty changePage }">
		<jsp:include page="${changePage }"></jsp:include>
</c:if>

加载头像设置:

private void userHead(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		//1.接受参数(头像名称)
		String imageName = request.getParameter("imageName");
		//2.判断是否为空
		if (StringUtil.isEmpty(imageName)) {
			return;
		}
		//3.得到文件存放服务器的真实路径 request.getServletContext().getRealPath("/WEB-INF/upload/"+头像名称);
		String filePath = request.getServletContext().getRealPath("/WEB-INF/upload/"+imageName);
		//4.通过路径得到file对象
		File file = new File(filePath);
		//5.判断file对象是否存在,并且是一个标准文件
		if (!file.exists()||!file.isFile()) {
			return;
		}
		//6.截取图片名称,得到图片后缀
		String pic = imageName.substring(imageName.lastIndexOf(".")+1,imageName.length());
		//判断是否为空
		if(StringUtil.isEmpty(pic)){
			return;
		}
		//7.根据不同后缀,设置对象的响应MIME类型
		if ("gif".equals(pic.toLowerCase())) {
			response.setContentType("image/gif");
		}else if("png".equals(pic.toLowerCase())){
			response.setContentType("image/png");
		}else if("jpg".equals(pic.toLowerCase())){
			response.setContentType("image/jpeg");
		}
		//8.利用commons-io的jar包,拷贝file对象 FileUtil.copyFile(file,response,getOutputStream());
		FileUtils.copyFile(file, response.getOutputStream());
	}

 

标签:pic,request,equals,项目分析,头像,imageName,file,response,加载
来源: https://blog.51cto.com/u_13985831/2836990