1. 程式人生 > 其它 >springmvc實現頭像上傳與頭像展示(jsp)

springmvc實現頭像上傳與頭像展示(jsp)

技術標籤:Java

使用頭像上傳功能完成註冊

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>