博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
毕业设计(二十六)--- 上传头像预览
阅读量:7081 次
发布时间:2019-06-28

本文共 2368 字,大约阅读时间需要 7 分钟。

hot3.png

网站注册使用的是快速注册, 没有上传头像,  使用的是默认头像, 在自己的空间可以修改头像,

使用弹出层,

弹出层前面写过, 就不介绍.

上传头像表单:  enctype="multipart/form-data"  这句千万不要忘记~~~~~

用户的headImage属性存放用户头像存放地址.

上传头像

表单提交后,处理代码: 为了防止一次次上传的图片过多, 在广告 头像 等需要上传图片的地方,更换图片后都会把  原图删除

public String uploadheadimages() throws Exception{		System.out.println("jinru");		Users u = (Users)ActionContext.getContext().getSession().get("user");				String oldpicAdd = u.getHeadImage();		// 把 图片路径放到对象中		String strPath = ServletActionContext.getServletContext().getRealPath(				"UserHeadImages");		// System.out.println(strPath);		// D:\tomcat6My8082\webapps\Myblog\admin\AdImages		File path = new File(strPath);		if (!path.exists()) {			path.mkdirs();		}		String uuid = UUID.randomUUID().toString();		String rt[] = upFileFileName.split("\\.");// 把文件名 abc.jpg 分成 rt[0]="abc"													// rt[1]="jpg"		// System.out.println(rt[1]);		// 用uuid 随机代替"abc"		upFileFileName = new String(uuid + "." + rt[1]);//		InputStream is = new FileInputStream(upFile);		OutputStream os = new FileOutputStream(new File(path + "/"				+ upFileFileName));		String headurl = "UserHeadImages" + "/" + upFileFileName;		headurl = headurl.replace('\\', '/');		// ActionContext.getContext().put("headurl", headurl);		System.out.println(headurl);// 存放图片路径								try {			int len;			byte[] buffer = new byte[1024];			while ((len = is.read(buffer)) > 0) {				os.write(buffer, 0, len);			}		} catch (Exception e) {			e.printStackTrace();		} finally {			if (is != null) {				is.close();			}			if (os != null) {				os.close();			}		}				u.setHeadImage(headurl);		manager.updateUser(u);				ActionContext.getContext().getSession().put("user", manager.findOneUsers(u.getId()));						if(oldpicAdd!=null){		// 删除上一次上传的广告图片		String oldpath = ServletActionContext.getServletContext().getRealPath(				oldpicAdd);		System.out.println(oldpath+"----------------");		File oldfile = new File(oldpath);		System.out.println(oldpath);		System.out.println(oldfile.isFile());		System.out.println(oldfile.exists());		if (oldfile.isFile() && oldfile.exists()) {			oldfile.delete();		}	}

 用户的信息是放到 session 中的.

用户头像的显示:  如果用户没有上传头像, 则显示默认头像

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/wangt10/blog/508394

你可能感兴趣的文章
linux自启动脚本
查看>>
Java 吸血鬼数字
查看>>
阿里P8架构师谈:高并发网站的监控系统选型、比较、核心监控指标
查看>>
【对讲机的那点事】做无线电天馈系统你会安装馈线接头吗?
查看>>
【云计算】入门级网站避免单点故障的云产品架构(适合Discu!论坛)
查看>>
Linux下最快速共享目录的方法
查看>>
RelativeSource 简述
查看>>
博弈论 斯坦福game theory stanford week 6.1_
查看>>
Silverlight & Blend动画设计系列十:Silverlight中的坐标系统(Coordinate System)与向量(Vector)运动...
查看>>
WPF 蒙罩层 LoadingPage
查看>>
冰火之中的VR者们过得还好吗?我们问了这些人
查看>>
[Leetcode] Power of Two and Power of Four 二之幂四之幂
查看>>
R之聚类分析法
查看>>
图形数据库公司 Neo4j 获得 E 轮 8000 万美元融资
查看>>
Oracle 截取、查找字符函数(持续更新)
查看>>
2018年杭州云栖大会-企业办公数据处理和分发(函数计算篇)
查看>>
Javascrip—拷贝对象(13)
查看>>
Flink在唯品会的实践
查看>>
在 Cent OS 7 上搭建带着 PHP 7 和 Memcached 的 LAMP
查看>>
Android线程——StackTraceElement线程运行栈的探索
查看>>