public void save(@RequestParam(value = "applicationImage", required = false) MultipartFile file, HttpServletResponse response,HttpServletRequest request,Entity entity) throws Exception{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String date=sdf.format(new Date()); String path = request.getSession().getServletContext() .getRealPath("image"+File.separator+date); String backupPath="bakimage"+File.separator+date; String []postfixs=file.getOriginalFilename().split("\\."); String postfix=postfixs[postfixs.length-1]; String fileName = new Date().getTime()+"."+postfix; File targetFile = new File(path, fileName); File backupFile = new File(backupPath, fileName); File backupFileDir=new File(backupPath); if(!backupFileDir.exists()){ backupFileDir.mkdirs(); } backupFile.createNewFile(); if (!targetFile.exists()) { targetFile.mkdirs(); } try { file.transferTo(targetFile); File f=new File(path+File.separator+fileName); FileUtils.copyFile(f, backupFile); entity.setImage(date+File.separator+fileName); if("".equals(entity.getId())||entity.getId()==null){ zealotService.save(entity); }else{ zealotService.update(entity); } } catch (Exception e) { e.printStackTrace(); } WebUtil.print(response, "success"); }
<!-- springMvc文件上传的支持 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1024000000" /> <property name="resolveLazily" value="true" /> <property name="maxInMemorySize" value="4096" /> </bean>
springmvc的文件上传与备份
最新推荐文章于 2024-09-11 15:28:11 发布