使用SpringMVC实现文件上传功能
来源:互联网 发布:python split 多个 编辑:程序博客网 时间:2024/05/21 15:43
最近做项目多次用到文件上传功能,并且项目底层框架使用的是springMVC,虽不难但容易忘记,在这里记录下(实现多文件动态上传(暂时完成,有缺陷):直接copy项目代码,仅供参考)
view页面代码如下:
<form:form id="inputForm" modelAttribute="userEnterprise" action="${ctx}/sys/userEnterprise/save" method="post" class="form-horizontal" enctype="multipart/form-data"> <tr> <td> <input name="docName" maxlength="255" class="required"/> </td> <td> <input name="infoFile" type="file" class="leg" /> </td> <td> <input name="remarks" maxlength="255" class="required "/> </td> </tr></form:form>
注意form标签必须含有enctype=”multipart/form-data”元素
controller代码如下:
@RequestMapping(value = "save") public String save(@RequestParam(value="infoFile",required=false)MultipartFile[] files , UserEnterprise userEnterprise, HttpServletRequest request , HttpServletResponse response , Model model, RedirectAttributes redirectAttributes) { if (!beanValidator(model, userEnterprise)){ return form(userEnterprise, model); } for (MultipartFile f : files) { boolean isSuccess = FileUtils.saveFileToFolder(f, FileUtils.readPropertiesFile("zhongdai.properties", "zhongdai_enterprise_user_document_disc")+"/"+userEnterprise.getUser().getId()); if(!isSuccess){ logger.debug("保存企业用户资料失败!"); addMessage(redirectAttributes, "保存企业用户资料失败!"); return "redirect:"+Global.getAdminPath()+"/sys/userEnterprise/?repage"; } } for (int i = 0; i < files.length; i++) { String docName = request.getParameter("docName"+i); String remarks = request.getParameter("remarks"+i); String subPath = FileUtils.readPropertiesFile("zhongdai.properties", "zhongdai_enterprise_user_document_db"); //获取保存地址目录 String path=subPath+"/"+userEnterprise.getUser().getId()+"/"+files[i].getOriginalFilename(); //拼接全路径 documentService.save(new UserEnterpriseDocument(userEnterprise,docName,path,new Date(),remarks)); } userEnterpriseService.save(userEnterprise); addMessage(redirectAttributes, "保存企业用户信息成功"); return "redirect:"+Global.getAdminPath()+"/sys/userEnterprise/?repage"; }
文件上传工具类如下:
public static boolean saveFileToFolder(MultipartFile mFile , String folder , String... rename){ boolean isSuccess=false; if(mFile.isEmpty()){ log.debug("文件为空"); return false; } try { //验证磁盘地址是否存在 File folderPath = new File(folder); if(!folderPath.exists()){ isSuccess= folderPath.mkdir(); //建一个文件夹 } //保存文件 mFile.transferTo(new File(folder,mFile.getOriginalFilename())); //获取文件保存路径 File file=new File(folderPath.toString(),mFile.getOriginalFilename()); //是否修改文件名 if(rename.length <= 0){ isSuccess=true; }else{ isSuccess = file.renameTo(new File(folderPath,rename[0])); } } catch (IOException e) { e.printStackTrace(); } return isSuccess; }
其他方式1():
单个文件上传也可以使用Ajax新版本支持FormData对象,FormData对象是html5的一个对象,目前的一些主流的浏览器都已经兼容
支持以下浏览器版本
1. Chrome 7+
2. Firefox 4.0 (2.0)
3. Internet Explorer 10+
4. Opera 12+
5. Safari 5+
这里不举例了,网上有简单易学使用教程
其他方式:使用jquery.form.js(转载):
<form id="tf"> <input type="file" name="img"/> <input type="text" name="username"/> <input type="button" value="提" onclick="test();"/></form>
下面使用jquery.form.js的表单插件来提交表单
$("#tf").ajaxSubmit();
额,就是这么简单,你也不要问我为什么就是这么简单,然后它就是会把整个表单,作为一个ajax来提交,同样的,它也支持文件上传!
0 1
- 使用SpringMVC实现文件上传功能
- springMVC实现上传文件功能
- 使用SpringMVC实现文件上传
- SpringMVC快速实现文件上传功能
- SpringMVC图片文件上传功能实现
- SpringMVC图片文件上传功能实现
- WebUploader+SpringMVC实现文件上传功能
- WebUploader+SpringMVC实现文件上传功能
- SpringMVC下实现多文件上传功能
- 文件上传功能(springMVC)
- springmvc上传文件功能
- 使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
- 使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
- SpringMVC使用MultipartFile 实现异步上传文件
- SpringMVC使用HttpClient实现文件上传
- 使用springmvc实现文件的上传
- SpringMVC使用CommonsMultipartResolver实现文件上传
- 使用 SpringMVC 实现文件下载功能
- JDNI学习总结(一)
- C语言模拟海战
- 如何使用SAP发送邮件?
- CSS box-shadow
- jQuery选择器总结
- 使用SpringMVC实现文件上传功能
- java设计模式_原型模式
- Docker数据管理:Named volume
- ABAP中对变式的处理
- 向手机磁盘存数据利用SharedPreferences
- poj 1039
- AngularJS 简介
- 网络基础知识点总结1
- 数组的函数传递的两种方式(数组名和指针)