springmvc实现头像上传与头像展示(jsp)

使用头像上传功能完成注册

Controller层代码

public String ZC(String name, String phone, String username, String pwd, MultipartFile file)
			throws IllegalStateException, IOException {
     

		// 定义保存数据库的路径
		String sqlpath = null;
		// 定义文件保存的本地路径
		String localPath = "D:\\xxx\\SSMDemo\\WebContent\\images\\";
		// 定义 文件名
		String filename = null;
		// 判断文件是否为空
		if (!file.isEmpty()) {
     
			// 生成uuid作为文件名称
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			// 获得文件类型(可以判断如果不是图片,禁止上传)
			String contentType = file.getContentType();
			// 获得文件后缀名
			String suffixName = contentType.substring(contentType.indexOf("/") + 1);
			// 得到 文件名
			filename = uuid + "." + suffixName;
			// 文件保存路径
			file.transferTo(new File(localPath + filename));
		}
		//	得到文件保存在本地的路径
		String xxx = localPath + filename;
		//	得到需要保存到数据库中的文件路径
		sqlpath = "/images/" + filename;
		//	System.out.println(xxx);
		//	System.out.println(sqlpath);
		VIP vip = new VIP();
		vip.setName(name);
		vip.setPhone(phone);
		vip.setUsername(username);
		vip.setPwd(pwd);
		vip.setTx(sqlpath);

		zhuCeServicee.register(vip);

		return "";

	}

domain实体类

package com.mvaweb.domain;

import org.springframework.web.multipart.MultipartFile;

public class VIP {
     
	public int id;
	public String name;
	public String phone;
	public String username;
	public String pwd;
	public String tx;//数据库中的路径
	public MultipartFile file;//实际保存的文件
	public int getId() {
     
		return id;
	}
	public void setId(int id) {
     
		this.id = id;
	}
	public String getName() {
     
		return name;
	}
	public void setName(String name) {
     
		this.name = name;
	}
	
	public String getPhone() {
     
		return phone;
	}
	public void setPhone(String phone) {
     
		this.phone = phone;
	}
	public String getUsername() {
     
		return username;
	}
	public void setUsername(String username) {
     
		this.username = username;
	}
	public String getPwd() {
     
		return pwd;
	}
	public void setPwd(String pwd) {
     
		this.pwd = pwd;
	}
	public String getTx() {
     
		return tx;
	}
	public void setTx(String tx) {
     
		this.tx = tx;
	}
	public MultipartFile getFile() {
     
		return file;
	}
	public void setFile(MultipartFile file) {
     
		this.file = file;
	}
	
	public VIP() {
     
		// TODO Auto-generated constructor stub
	}
	public VIP(int id, String name, String phone, String username, String pwd, String tx) {
     
		super();
		this.id = id;
		this.name = name;
		this.phone = phone;
		this.username = username;
		this.pwd = pwd;
		this.tx = tx;
	}
	
}

登录功能实现头像展示

@Autowired
	private LoginServiceImpl LoginServiceImpl;
	@RequestMapping("/login")
	public String login(String username, String pwd, HttpServletRequest req, HttpSession session) {
     
		ModelAndView mav = new ModelAndView();
		VIP vip = LoginServiceImpl.login(username);
		//System.out.println(vip.getTx());
		if (vip == null) {
     
			return "用户名不正确";
		} else if (!vip.getPwd().equals(pwd)) {
     
			return "密码不正确";
		} else {
     
			session.setAttribute("username", username);
			session.setAttribute("vip", vip.getTx());
			return "loginsuccess";
		}

	}

jsp页面获取用户名和头像信息

<body>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
System.out.println(basePath);

%>
<h3>欢迎使用本系统h3>
<div class="imgs">
<img  src="<%=basePath%>${vip}">

div>
用户名:${username}
body>

你可能感兴趣的:(Java)