上传头像

一、使用上传插件上传头像,简单实用。


二、 注意地方: 在控制层的 MultipartFile file  参数跟上传框中的name一样。在代码处详细标明


三、下载地址  点击打开链接


效果图  






实体类

public class User {
	
	private int id;
	private String name;
	private String password;
	private String photourl;
	
	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 getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPhotourl() {
		return photourl;
	}
	public void setPhotourl(String photourl) {
		this.photourl = photourl;
	}

}
 mapper层

public interface UserMapper {
	
	//登录系统
	public User loginUser(String name, String password);
	//查询信息
	public User selectUserById(int id);
	//更新头像
	public int updatePhoto(User user);
}

映射文件
<mapper namespace="com.mapper.UserMapper">

	<!-- 登录 -->
	<select id="loginUser" resultType="com.entity.User">
		select * from user where name = #{0} and password = #{1}
	</select>
	
	<!-- 查询信息 -->
	<select id="selectUserById" resultType="com.entity.User">
		select * from user where id = #{id}
	</select>

	<!-- 更新头像 -->
	<update id="updatePhoto" parameterType="com.entity.User">
		update user set photourl = #{photourl} where id = #{id}
	</update>
</mapper>

控制层

@Controller
public class UserController {

		@Autowired
		private UserService userservice;

		//登录
		@RequestMapping("/login.action")
		public String loginUser(String name, String password,HttpSession session){
			if("".equals(name) || "".equals(password)){
				return "index.jsp";
			}else{
				User user = userservice.loginUser(name, password);
				if(user == null){
					return "index.jsp";
				}else{
					session.setAttribute("user", user);
					return "two.jsp";
				}
			}
		}
		
		@RequestMapping("/upload.action")
		public String updatePhoto(User user,HttpServletRequest request, MultipartFile file) {//MultipartFile file  file 是上传框中的name,不必跟数据库中一样,file需要在控制层使用
			int id = ((User) request.getSession().getAttribute("user")).getId();
			//获取上传的文件名称
			if (file.getOriginalFilename() != "") {
				// 获取上传文件的保存目录
				String name = ((User) request.getSession().getAttribute("user")).getName();
				//获取servet容器,也就是tomcat容器   "/" 代表根目录   上传的头像在根目录下创建加上用户名
				
				//上传的路径是根目录创建用户名的文件夹,放在根目录下
				//String pathName = request.getSession().getServletContext().getRealPath("/") + name;
				
				//在根目录下创建photo 文件夹,在文件夹里创建用户名的文件夹,把图片放在里面
				String pathName = request.getSession().getServletContext().getRealPath("/") + "photo/" + name;
				File path = new File(pathName);
				// 如果目录不存在,则创建该目录!
				if (!path.exists()) {
					path.mkdirs();
				}
				String fileName = null;
				// 生成随机的唯一的文件名
				// getName:获取控件的名称
				// getOriginalFilename:获取被上传的文件名
				fileName = UUID.randomUUID().toString() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				// 创建新文件的对象
				File newFile = new File(path, fileName);
				try {
					file.transferTo(newFile);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				//存进数据库中的图片地址
				String photourl = "photo/" + name + "/" + fileName;
				user.setPhotourl(photourl);
				user.setId(id);
			}
			userservice.updatePhoto(user);
			User u = userservice.selectUserById(user.getId());
			request.getSession().setAttribute("user", u);

			return "two.jsp";
		}
		

jsp页面

<body>
   	<table>
   		<tr>
   			<td>用户名:</td>
   			<td>${user.name }</td>
   		</tr>
   		<tr>
   			<td>头像:</td>
   			<td>
   				<img height=100 src="${user.photourl }" width=90>   显示头像
   			</td>
   		</tr>
   		<tr>
   			<td>
	  			<a href="upload.jsp">上传头像</a>
	  		</td>
   		</tr>
   	</table>
  </body>


<body>
	<form action="upload.action" method="post" enctype="multipart/form-data">
		<input type="file" name="f"/>
		<input type="submit" value="上传" />
	</form>
</body>

PS: 小功能自己业余时间写的,方法不止一种

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值