毕业设计(二十六)--- 上传头像预览

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

使用弹出层,

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


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

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

<form id="form1" enctype="multipart/form-data" action="user!uploadheadimages" method="post" style="margin:0px auto;" >
		
<br>
上传头像<input type="file" id="upfile" name="upFile" οnchange="onUploadImgChange(this)"  />
<br/>
<div id="preview_wrapper">       
        <div id="preview_fake">       
            <img id="preview" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" οnlοad="onPreviewLoad(this)"/>       
        </div>       
    </div>  
    <br/>
<input type="button" value="上传" οnclick="checkFile2(); " />		
		
		<img id="preview_size_fake"/>   
		
		</form>

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

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 中的.

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

<s:if test="#session.user.headImage!=null">
    <a href="myzone"><img src="<%=basePath %>${ user.headImage}" width="120px" height="120px"  /></a>
			</s:if>
			<s:else>
	<a href="myzone"><img src="<%=basePath%>public/image/morentouxiang.png" width="120px" height="120px"  /></a>
			</s:else>



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值