单张图片上传表单提交

freemarker

<!DOCTYPE html>
<html lang="zh-cn" class="hb-loaded">

<head>
   <title>简洁大气快速登录注册模板</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="js/zepto.min.js"></script>
<script type="text/javascript" src="js/zepto.alert.js"></script>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
	<script type="text/javascript">    
	 	function preview(file){  
	 		var prevDiv = document.getElementById('preview');  
	 		if (file.files && file.files[0]){  
	 			var reader = new FileReader();  
	 			reader.onload = function(evt){  
					prevDiv.innerHTML = '<img src="' + evt.target.result + '" />';  
				}    
				reader.readAsDataURL(file.files[0]);  
			}else{  
				prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';  
			}  
	 	}  
	</script> 
    <script >
		function tologin(){
			if($('#username').val()==''){
				$.dialog({
	                content: '请输入用户名',
	                time: 1500,
            	});
				return;
			}
			if($('#pwd').val()==''){
				$.dialog({
	                content: '请输入密码',
	                time: 1500,
            	});
				return;		
			}
	    	window.location.href = 'login.action?username='+$('#username').val()+'&pwd='+$('#pwd').val();
		}
		function regist(){
			if($('#reusername').val()==''){
				$.dialog({
	                content: '请输入用户名',
	                time: 1500,
            	});
				return;
			}
			if($('#repwd').val()==''){
				$.dialog({
	                content: '请输入密码',
	                time: 1500,
            	});
				return;		
			}
			if($('#reconpwd').val()==''){
				$.dialog({
	                content: '请输入密码',
	                time: 1500,
            	});
				return;		
			}
			if($('#reconpwd').val()!=$('#repwd').val()){
				$.dialog({
	                content: '两次密码不一致',
	                time: 1500,
            	});
				return;		
			}
			$.ajax({                                        
			url:"regist.action",
			dataType: "json",
			data: {"username":$('#reusername').val(),"pwd":$('#repwd').val(),"confirmPwd":$('#reconpwd').val()},
			type: 'post',                         
			cache: false,                         
			async: false,                         
			success: function(data){  
				if(data == '0')
					alert("注册成功!"); 
					window.location.href="/admin/index.action";
				if(data == '1')
					alert("注册失败");
			},                                    
			error:function(data){     
				alert("注册失败err!");                                                   
			}                                     
		});
		}
	</script>
</head>

<body>
    <h1>逸得科技后台帐户注册<sup>2016</sup></h1>
    <form id="picForm" action="uploadPic.action" method="post" enctype="multipart/form-data" target="iframeInfo">
		<div id="preview"></div> 
		<input type="file" name="file" id="file" οnchange="preview(this)"/>
		<input type="submit" value="上传"/>
	</form>
</body>

</html>

Controller


@RequestMapping(value="uploadPic",method = RequestMethod.POST)
	public String upload_yz(ModelMap map,@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {

		String uploadUrl = uploadService.uploadFile(file, request);
		try {
		}catch(Exception e){
		}
		return "";
	}

Service

/**
	 * 上传文件
	 * @param file	文件
	 * @param folder	文件夹名称
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public String uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String rootPath = request.getRealPath("/");
		String prefix = uploadUrlsConfigurer.getContextProperty(request);
		String datePath = format.format(new Date());
		String appPath = prefix+"/"+datePath;
		FileUtils.createFilePath(request.getRealPath("/"), appPath);
		String fileName = Identities.uuid2() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		/*
		 * /upload/other/2015-04-15/cea78319e0ea4756b29de05b2cc431ab.pdf
		 * */
		FileCopyUtils.copy(file.getBytes(), new File(request.getRealPath("/") + appPath+"/"+fileName));
		return "/" + appPath+"/"+fileName;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值