记录自己的上传头像功能

jsp

<style type="text/css">
		#t1{border:solid #F3F3F3; border-width:1px 0px 0px 1px;}
		#t1 tr{height:35px}
		#t1 td{border:solid #F3F3F3; border-width:0px 1px 1px 0px;}
		#td_l{background-color:#F3F3F3;text-align: right;}
    	.datagrid-row:nth-child(2n){
		    background:#FFFFFF;
		}
		.upload-img{
			position: relative;
			float: left;
			margin: 0px;
			overflow: hidden;
		}
		.upload-img img{
			width: 230px;
			height: 160px;
		}
		.comment-pic-upd{
			position: absolute;
			top: 0;
			left: -80;
			z-index: 100;
			width: 230px;
			height: 160px;
			filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);
			filter:alpha(opacity=0);
			-moz-opacity:0;
			-khtml-opacity: 0;
			opacity:0;
			background: none;
			border: none;
			cursor: pointer;
		}
	</style>
	
	</head>
	<body >
		<div id="container" class="main" >
			<div class="barTitle">
				<div class="content"><span>修改人员</span>
				<div class="btn_div" style="float:right;margin:5px;line-height:0;">
				<input type="button" class="returnlist_btn" value="" onclick="_back()"/>
					</div> 
				</div>
			</div>
		
	<form name="myForm" method="post" enctype="multipart/form-data" style="margin:0px">	
		<table border="0" cellspacing="5px;" cellpadding="0" style="margin-top:0px;">
			<tr>
				<td align="right" width="120px;">姓名: 
					<input type="hidden"  id="personuuid" name="personuuid" value="${info.PERSONUUID }" />
				</td>
				<td width="380px;"><input id="username"type="text"  name="username" readonly value="${info.USER_NAME}" style="width:170px;height:30px;vertical-align:middle;" />
				 <!-- <input type="button"  class="checkPer_btn" onclick="checkPer()"  style="vertical-align: middle;"/> -->
				</td>
				<td align="right" width="120px;">单位: </td>
				<td width="180px;">	
				<input id="unitname"type="text"  name="unitname" readonly value="${info.UNIT_NAME}" style="width:170px;height:30px;vertical-align:middle;" />
				</td>	
				<td width="120px;" align="right" >性别:</td>
				<td width="180px;" ><select style="width: 175px;height: 38px;" id="sex" name="sex">
				<option value="1" ${info.SEX=="1"?"selected":""} >男</option>
				<option value="2" ${info.SEX=="2"?"selected":""}>女</option>
				</select>
				</td>
			</tr>
			<tr>
			<td align="right" valign="top">个人照片:</td>
			<td class="td_l"  align="left">
						<div style="width:230px;height:160px;">
	         				<div id="imgDiv" class="upload-img" style="height:100%;">
	           					<input type="file" id="employeePhoto" name="employeePhoto" class="comment-pic-upd" onchange="addEmployeePhoto()" />
								<c:choose>
									<c:when test="${flag=='no'}">
								<img id="employeeImg" src="<%=contextPath%>/include/images/addPer.png" alt="上传照片" title="">
									</c:when>
								</c:choose>
								<c:choose>
									<c:when test="${flag=='ok'}">
								<img id="employeeImg" src="<%=contextPath%>/check/enforcement_getImg.do?url=${picInfo.URL}" alt="${picInfo.FILE_NAME }" title="">
									</c:when>
								</c:choose>
							</div>
		            	</div>
					</td>
			</tr>
			<tr>
			<td colspan="6">
				<div class="" id="pagetitle" align="center">
					<div class="btn_div" style="line-height:0;">
				   	    <input type="button" id="btnBatchPass" class="confirm_btn" value="确定" onclick="_confirm()" style="vertical-align: middle;"/>
						<input type="button" id="btnBatchReturn" class="close_btn" value="返回" onclick="_back()" style="vertical-align: middle;"/>
					</div>
				</div>
			</td>
			</tr>
	</table>
	</form>

js

/*****************上传照片******************/
function addEmployeePhoto(){
	var file = document.getElementById("employeePhoto");
	var pic = document.getElementById("employeeImg");
	//var bannerPic = document.getElementById("shopBannerImg");
	imgUpload(file, pic);
	//imgUpload(file, bannerPic);
	
}
function imgUpload(file, pic){
	var ext = file.value.substring(file.value.lastIndexOf(".") + 1).toLowerCase();
	// gif在IE浏览器暂时无法显示
	if (ext != 'png' && ext != 'jpg' && ext != 'jpeg') {
		$.messager.alert('消息提示','图片的格式必须为png或者jpg或者jpeg格式!', 'info');  
		return false;
	}
	var isIE = navigator.userAgent.match(/MSIE/) != null,
	isIE6 = navigator.userAgent.match(/MSIE 6.0/) != null;
	
	if (isIE) {
		file.select();
		var reallocalpath = document.selection.createRange().text;
		// IE6浏览器设置img的src为本地路径可以直接显示图片
		if (isIE6) {
			pic.src = reallocalpath;
		} else {
			// 非IE6版本的IE由于安全问题直接设置img的src无法显示本地图片,但是可以通过滤镜来实现
			pic.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src=\"" + reallocalpath + "\")";
			// 设置img的src为base64编码的透明图片 取消显示浏览器默认图片
			pic.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
		}
	} else {
		html5Reader(file, pic);
	}
}

function html5Reader(file, pic) {
	var file = file.files[0];
	var reader = new FileReader();
	reader.readAsDataURL(file);
	reader.onload = function(e) {
		pic.src = this.result;
	}
}
/保存/
function _confirm(){
var url = 'enforcement_modify.do';
	myForm.action=url;
	myForm.submit();
}

后台Action


	private File employeePhoto;
	private String employeePhotoFileName;
	
	public File getEmployeePhoto() {
		return employeePhoto;
	}


//修改保存的方法
@SuppressWarnings({ "unused", "rawtypes" })
	public String modify(){
		try {
			String personuuid=request.getParameter("personuuid");
			File employeeFile = getEmployeePhoto();
			String photoUUID =personuuid;
			String url="";
			if (employeeFile != null) {//图片存在
				String photoName =  getEmployeePhotoFileName();
				String exp = photoName.substring(photoName.lastIndexOf("."),photoName.length());
				//photoUUID = KeyUtil.uuid();
				String rootPath = SaveAndroidSubmitFileUtil.FILE_PATH;
            	String employeePath = Configuration.getString("unLicense_shopPic") + DateUtil.getCurrentDate("yyyyMMdd") + "/employeePic/";
            	File pathDir = new File(rootPath + employeePath);
				if (!pathDir.exists() || pathDir == null) {
					// 判断指定路径dir是否存在,不存在则创建路径
					pathDir.mkdirs();
				}
                FileOutputStream fos = new FileOutputStream(rootPath + employeePath + File.separator + photoUUID + exp);
                FileInputStream fis = new FileInputStream(employeeFile);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fis.close();
                fos.close();
                Map m = new HashMap();
                m.put("uuid", personuuid);
                m.put("groupUUID", personuuid);
                m.put("name", photoName);
                m.put("type", "0");
                m.put("url", employeePath+photoUUID+exp);
                m.put("isVaild", "1");
                enforcementService.updateNoCardCustomerPicGroup(m);
	        }	
			String unitname=request.getParameter("unitname");
			String sex=request.getParameter("sex");
			String ident=request.getParameter("ident");
			String birthDay=request.getParameter("birthDay");
			String mobile=request.getParameter("mobile");
			String lawno=request.getParameter("lawno");
			String lawstation=request.getParameter("lawstation");
			String skilllevel=request.getParameter("skilllevel");
			String other=request.getParameter("other");
			Map map=new HashMap();
			map.put("personuuid", personuuid);
			map.put("unitname", unitname);
			map.put("sex", sex);
			map.put("ident", ident);
			map.put("birthDay", birthDay);
			map.put("mobile", mobile);
			map.put("lawno", lawno);
			map.put("lawstation", lawstation);
			map.put("skilllevel", skilllevel);
			map.put("other", other);
			enforcementService.modify(map);
		} catch (Exception e) {
			LogUtil.error(e);
			e.printStackTrace();
		}
		return "initUI";
	}



/**
	*@ title:查询个人图片---用于展示图片---前台jsp的img标签访问该方法
	*@ Description: 组建json构建页面列表
	*@ param: null
	*@ return null
	*@ throw 异常
	*@ author:lixy
	*@ date:2016-1-5
	*@ modify Description 修改内容描述
	*@ modify man:修改人
	*@ modify date:修改时间(yyyy-MM-dd)
	*/
	public void getImg() {
		InputStream inputstreamt = null;
        BufferedInputStream bufferedinputstream = null;
		try {
			String rootPath = SaveAndroidSubmitFileUtil.FILE_PATH;
			String url = new String(request.getParameter("url").getBytes("ISO-8859-1"),"UTF-8");
			String path = rootPath + url;
			System.out.println("------path---------"+path);
			File file1 = new File(path);
			inputstreamt = new FileInputStream(file1); 
			bufferedinputstream = new BufferedInputStream(inputstreamt);
			ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(bytearrayoutputstream);
            for(int i = -1; (i = bufferedinputstream.read()) != -1;){
                bufferedoutputstream.write(i);
            }
            bufferedoutputstream.flush();
            bytearrayoutputstream.flush();
            response.getOutputStream().write(bytearrayoutputstream.toByteArray());
            response.flushBuffer();
		} catch (Exception ex) {
			LogUtil.error(ex);
		}finally{
            try{
                if(inputstreamt != null){inputstreamt.close();}
                if(bufferedinputstream != null){bufferedinputstream.close();}
            }catch(Exception e){
            	LogUtil.error(e);
            }
        }
	}

 

转载于:https://my.oschina.net/u/3053442/blog/870104

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值