public static void upload(String strPath, ServletConfig config, HttpServletRequest request, HttpServletResponse response) { File dir = new File(strPath); String w = JdjWsxydUpload.getFilesRecursive(dir); if (w.equals("ligang")) { } else { strPath += "/" + w; File fileDir = new File(strPath); if (fileDir.exists()) { System.out.println("删除文件信息成功!!!"); fileDir.delete(); } } SmartUpload su = new SmartUpload(); try { su.initialize(config, request, response); su.upload(); for (int i = 0; i < su.getFiles().getCount(); i++) { com.jspsmart.upload.File file = su.getFiles().getFile(i); // 若文件不存在则继续 if (file.isMissing()) continue; // 将文件另存 file .saveAs("/webgroup/jdj/wsxyd/" + JdjWsxydUpload.getOneName() + "." + file.getFileExt()); } } catch (SmartUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } private static String getOneName() { String name = ""; name = "wsxyd" + System.currentTimeMillis(); return name; } public static String getFilesRecursive(File dir) { if (dir.isDirectory()) { String sub[] = dir.list(); if (sub.length > 0) { return sub[0]; } else { return "ligang"; } } else { return "ligang"; } } public static void main(String[] args){ File dir = new File("C:/windows/Help"); String w = JdjWsxydUpload.getFilesRecursive(dir); System.out.println(w); }
Help文件夹下有Corporate等文件夹,所以dir.list()返回的是Help文件夹下的文件夹名字的列表