Spring MVC实现文件上传
2017-12-15 12:23
267 查看
依赖jar包:
commons-io
commons-fileupload
commons-lang
Spring MVC配置文件中配置MultipartResolver:
前端form表单添加enctype="multipart/form-data"
页面添加:
控制器:
添加js,优化用户体验:
commons-io
commons-fileupload
commons-lang
Spring MVC配置文件中配置MultipartResolver:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 配置MultipartResolver,用于上传文件,使用Spring的CommonsMultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxInMemorySize" value="5000000"></property><!-- 文件上传大小,字节为单位 --> <property name="defaultEncoding" value="UTF-8"></property><!-- 请求的编码格式,需与jsp的pageEncoding一致 --> </bean> </beans>
前端form表单添加enctype="multipart/form-data"
页面添加:
</div> <div> <input type="hidden" id="errorinfo_lic" value="${uploadlicError }"/> <label for="companyLicPicPath">企业营业执照存储路径</label> <input type="file" name="attachs" id="companyLicPicPath"/> <font color="red"></font> </div> <div> <input type="hidden" id="errorinfo_Code" value="${uploadCodeError }"/> <label for="orgCodePicPath">组织机构代码证照片</label> <input type="file" name="attachs" id="orgCodePicPath"/> <font color="red"></font> </div>
控制器:
/** * 保存供应商信息 * * @param provider * @param session * @return * 文件上传流程:Spring MVC会将上传文件绑定到MultipartFile对象中,MultipartFile提供了获取上传文件内容、文件名等 * 最后通过其transferTo()方法将文件存储到服务器上 */ @RequestMapping("addProviderSave") public String addProviderSave( Provider provider, HttpSession session, HttpServletRequest request,//MultipartFile对象作为控制器处理方法的入参,数组必须加@RequestParam注解 @RequestParam(value = "attachs", required = false) MultipartFile[] attachs) { String companyLicPicPath = null;// String orgCodePicPath = null; String errorinfo = null; boolean flag = true; String path = request.getSession().getServletContext()//文件上传的目标路径(服务器),File.separator自适应路径分隔符 .getRealPath("statics" + File.separator + "uploadFiles"); for (int i = 0; i < attachs.length; i++) { MultipartFile attach = attachs[i]; if (!attach.isEmpty()) {//判断是否有文件上传 if (i == 0) {//数组里的文件对象是按照是按照form表单的file标签顺序进行存储的 errorinfo = "uploadlicError"; } else if (i == 1) { errorinfo = "uploadcodeError"; } String oldFileName = attach.getOriginalFilename();// 原文件名 String prefix = FilenameUtils.getExtension(oldFileName);// 后缀 int fileSize = 500000; if (attach.getSize() > fileSize) { request.setAttribute(errorinfo, "上传大小不得超过500KB"); flag = false; } else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")) { //当前系统时间+随机数+_personal.jpg,保证不会重复 String fileName= 4000 System.currentTimeMillis()+RandomUtils.nextInt(0,1000000)+"_personal.jpg"; File targetFile=new File(path,fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { attach.transferTo(targetFile);//把MultipartFile中文件流的数据输出至目标文件夹 } catch (IllegalStateException e) { e.printStackTrace(); request.setAttribute(errorinfo, "上传失败"); flag=false; } catch (IOException e) { e.printStackTrace(); } if(i==0){ companyLicPicPath=fileName;//数据库中存储文件名 }else if(i==1){ orgCodePicPath=fileName; } }else { request.setAttribute(errorinfo, "上传图片格式不正确"); flag=false; } } } if (flag) { provider.setCreatedBy(((User) session .getAttribute(Constants.USER_SESSION)).getId()); provider.setCreationDate(new Date()); provider.setCompanyLicPicPath(companyLicPicPath); provider.setOrgCodePicPath(orgCodePicPath); try { if (providerService.addProvider(provider) > 0) { return "redirect:/providerController/providerList"; } } catch (Exception e) { e.printStackTrace(); } } return "provideradd"; }
添加js,优化用户体验:
var companyLicPicPath=null; var orgCodePicPath=null; var errorinfo_lic=null; var errorinfo_Code=null; $(function(){ companyLicPicPath=$("#companyLicPicPath"); orgCodePicPath=$("#orgCodePicPath"); errorinfo_lic=$("#errorinfo_lic"); errorinfo_Code=$("#errorinfo_Code"); if(errorinfo_lic.val()==null||errorinfo_lic.val()==""){ companyLicPicPath.next().html("*上传文件类型必须是jpg、jpeg、png、pneg*"); }else{ companyLicPicPath.next().html(errorinfo_lic.val()); } if(errorinfo_Code.val()==null||errorinfo_Code.val()==""){ orgCodePicPath.next().html("*上传文件类型必须是jpg、jpeg、png、pneg*"); }else{ orgCodePicPath.next().html(errorinfo_Code.val()); }
});
相关文章推荐
- Spring MVC 4使用Servlet 3 MultiPartConfigElement实现文件上传(带源码)
- spring mvc 实现文件上传下载
- spring mvc + xmlHttpRequest2.0 实现无刷新上传文件,带进度条和剩余时间
- Spring MVC 实现文件上传功能
- spring mvc文件上传实现进度条
- Spring MVC使用ajax实现文件的上传,并将上传记录添加到数据库
- Spring MVC实现文件上传
- uploadify插件实现文件多媒体文件上传(spring mvc)
- SpringMVC创建web项目基础之(三)——Spring MVC实现文件上传
- spring mvc 实现任意文件上传—— 下载<二>
- Spring MVC 全注解的方式实现单个和多个文件上传
- Spring MVC 监听文件上传进度,实现上传进度条
- spring MVC实现文件上传
- 基于Spring Mvc实现的Excel文件上传下载示例
- Spring MVC 全注解的方式实现单个和多个文件上传
- Spring MVC实现上传文件报错解决方案
- spring mvc 实现多文件上传,且同时包含表单信息
- 基于Spring Mvc实现的Excel文件上传下载
- Spring MVC代码实例系列-10:Spring MVC实现简单的文件上传和下载
- 基于Spring MVC实现基于form表单上传Excel文件,批量导入数据