Map接受java
/** * 經過;spring 的方法解析全部方法 * @param multipartRequest 請求 * @param keys 獲取參數key 對應的文件 key的值必須和前臺頁面的name值同樣 * @param systemPath 系統路徑(活路徑) * @param path 本身目錄的路徑 * @return */ public static String[] getFileURL(MultipartHttpServletRequest multipartRequest, String[] keys,String systemPath,String path){ Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); String[] vales=new String[keys.length]; for (int i=0;i<keys.length;i++) { vales[i]=getPathValue(fileMap.get(keys[i]),systemPath,path); } return vales; }
protected static String getPathValue(MultipartFile file,String systemPath,String path){ if (!file.isEmpty()) { try { String fileName=file.getOriginalFilename(); if(StringUtils.isNotBlank(fileName)){ String[] str=fileName.split("\\."); if(str.length==1){ path=path+new Date().getTime()+str[0]+".JPEG"; }else{ path=path+new Date().getTime()+"."+str[str.length-1]; } } // 文件保存路徑 File fl=new File(systemPath+path); if(!fl.getParentFile().exists()){ fl.getParentFile().mkdirs(); } // 轉存文件 file.transferTo(fl); } catch (Exception e) { e.printStackTrace(); } }else{ return null; } return path; }
多文件一個文件名spring
MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request; Iterator<String> ite=mr.getFileNames(); while (ite.hasNext()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } String name = ite.next(); List<MultipartFile> file = mr.getFiles(name); if (file != null || file.size() != 0) { for (MultipartFile files : file) { AppOpinionImgEntity opinionImgEntity = new AppOpinionImgEntity(); opinionImgEntity.setCreateBy("1"); opinionImgEntity.setCreateDate(DateTimeUtils.getDateTime()); opinionImgEntity.setUpdateBy("1"); opinionImgEntity.setUpdateDate(DateTimeUtils.getDateTime()); opinionImgEntity.setDelTag("0"); String sysPath = request.getSession().getServletContext().getRealPath("/"); String path = "upload/opinion/"; try { String fileName = files.getOriginalFilename(); opinionImgEntity.setImgName(fileName); opinionImgEntity.setOpinionId(opinionEntity.getId()); if (StringUtils.isNotBlank(fileName)) { String[] str = fileName.split("\\."); if (str.length == 1) { path = path + new Date().getTime() + str[0] + ".JPEG"; opinionImgEntity.setImgType("JPEG"); } else { path = path + new Date().getTime() + "." + str[str.length - 1]; opinionImgEntity.setImgType(fileName.substring(fileName.lastIndexOf("."))); } } // 文件保存路徑 File fl = new File(sysPath + path); if (!fl.getParentFile().exists()) { fl.getParentFile().mkdirs(); } // 轉存文件 files.transferTo(fl); } catch (Exception e) { e.printStackTrace(); return new HashMap<String, Object>(); } opinionImgEntity.setImgUrl(path); opinionImgDao.save(opinionImgEntity); } } }