请求需要传入文件夹路径
这是后台代码
importjava.io.File;importjava.io.IOException;importjava.net.MalformedURLException;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;/*** 文件预览辅助类
*@authorzhy
**/@Controllerpublic classFileBrowseUtil {/*** 通过ajax请求获取传入的文件路径里边的文件fileList数组
*@paramreq
*@paramresp
*@paramparams 文件夹路径参数
*@return*@throwsServletException
*@throwsIOException
*@throwsMalformedURLException*/@RequestMapping("/getFileList")
@ResponseBodyprotected ArrayListCalculateGeoServlet(HttpServletRequest req,
HttpServletResponse resp,String params)throwsServletException, IOException,
MalformedURLException {
ArrayList fileList=new ArrayList();
String dir=req.getSession().getServletContext().getRealPath(params);
fileList=getFiles(dir);returnfileList;
}/*** 通过递归得到某一路径下所有的目录及其文件
*@paramfilePath 文件路径
*@return
*/
public ArrayListgetFiles(String filePath) {
ArrayList fileList = new ArrayList();
File root= newFile(filePath);
File[] files=root.listFiles();for(File file : files) {if(file.isDirectory()) {/** 递归调用*/getFiles(file.getAbsolutePath());
fileList.add(file.getAbsolutePath());
}else{
String picPathStr=file.getAbsolutePath();//String picPathStr = file.getAbsolutePath().replaceAll("\\\\","//");
fileList.add(getFileNameWithSuffix(picPathStr));
}
}/*for(String str:fileList){
System.out.println(str);
}*/
returnfileList;
}/*** 保留文件名及后缀*/
publicString getFileNameWithSuffix(String pathandname) {int start = pathandname.lastIndexOf("\\");if (start != -1) {return pathandname.substring(start + 1);
}else{return null;
}
}/*** 仅保留文件名不保留后缀*/
publicString getFileName(String pathandname) {int start = pathandname.lastIndexOf("\\");int end = pathandname.lastIndexOf(".");if (start != -1 && end != -1) {return pathandname.substring(start + 1, end);
}else{return null;
}
}
}