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;
}