5ad3170071ce3aab7d42b92f1cfbeb494cff09ea.svn-base 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <%@ page language="java" pageEncoding="utf-8"%>
  2. <%@ page import="java.util.*"%>
  3. <%@ page import="java.io.*"%>
  4. <%@ page import="javax.servlet.ServletContext"%>
  5. <%@ page import="javax.servlet.http.HttpServletRequest"%>
  6. <%
  7. //仅做示例用,请自行修改
  8. String path = "upload";
  9. String imgStr ="";
  10. String realpath = getRealPath(request,path)+"/"+path;
  11. List<File> files = getFiles(realpath,new ArrayList());
  12. for(File file :files ){
  13. imgStr+=file.getPath().replace(getRealPath(request,path),"")+"ue_separate_ue";
  14. }
  15. if(imgStr!=""){
  16. imgStr = imgStr.substring(0,imgStr.lastIndexOf("ue_separate_ue")).replace(File.separator, "/").trim();
  17. }
  18. out.print(imgStr);
  19. %>
  20. <%!
  21. public List getFiles(String realpath, List files) {
  22. File realFile = new File(realpath);
  23. if (realFile.isDirectory()) {
  24. File[] subfiles = realFile.listFiles();
  25. for(File file :subfiles ){
  26. if(file.isDirectory()){
  27. getFiles(file.getAbsolutePath(),files);
  28. }else{
  29. if(!getFileType(file.getName()).equals("")) {
  30. files.add(file);
  31. }
  32. }
  33. }
  34. }
  35. return files;
  36. }
  37. public String getRealPath(HttpServletRequest request,String path){
  38. ServletContext application = request.getSession().getServletContext();
  39. String str = application.getRealPath(request.getServletPath());
  40. return new File(str).getParent();
  41. }
  42. public String getFileType(String fileName){
  43. String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
  44. Iterator<String> type = Arrays.asList(fileType).iterator();
  45. while(type.hasNext()){
  46. String t = type.next();
  47. if(fileName.endsWith(t)){
  48. return t;
  49. }
  50. }
  51. return "";
  52. }
  53. %>