本文主要是介绍单张图片上传表单提交,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}
这篇关于单张图片上传表单提交的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!