1.Servlet位运算上传目录,两级目录 //在upload总目录下,创建2层子目录,用于存上传的文件,分散总目录压力,子目录级数不限 private String makeSubUpload(String uploadPath,String uuidRealFileName) throws FileNotFoundException, IOException{ //获取hashCode整型值 int code = uuidRealFileName.hashCode(); //第一级子目录 int dir1 = code & 0xF;//12 //第二级子目录 int dir2 = ( code >> 1 ) & 0xF;//6 //创建这些子目录 File file = new File(uploadPath+"/"+dir1+"/"+dir2); //如果不存在该子目录 if(!file.exists()){ //连续创建2个子目录 file.mkdirs(); } System.out.println("程序创建目录后,保存......"); Properties prop = new Properties(); String filePath = UploadServlet.class.getResource("/upload.properties").getFile(); prop.load(new FileInputStream(filePath)); prop.setProperty("subUploadPath",file.getPath()); prop.store(new FileOutputStream(filePath), "Update subUploadPath'"+file.getPath()+"'"); //将创建后的子目录返回 return file.getPath();//可不可以返回一个路径 } 2.获取时判断逻辑每个目录5张图片 /**文件目录路径*/ String uploadPath =this.getServletContext().getRealPath("/WEB-INF/upload"); //request.getContextPath()+"/upload"; //默认用配置信息 String subUploadPath = PropertiesUtil.getProperty("subUploadPath"); //如果配置信息为空,文件数>=5,重新赋值 if(subUploadPath==null||"".equals(subUploadPath)){ subUploadPath = makeSubUploadPath(uploadPath,uuidRealFileName); }else if(subUploadPath!=null&&!"".equals(subUploadPath)){ File lastFile = new File(subUploadPath); if(lastFile.exists()&&lastFile.list().length>=5){ subUploadPath = makeSubUploadPath(uploadPath,uuidRealFileName); } }