1.jsp页面中输入访问目录地址
<div> 下载日志 <form action="<%=request.getContextPath()%>/downloads" method="get" commandName="src" role="form" name="src"> <input type="text" class="form-control" id="src" name="src" placeholder="Enter src:" required/> <button type="submit" class="btn btn-sm btn-success">提交</button> </form> </div>2.java代码
//根据路径找到目录 @RequestMapping(value = "/downloads", method = RequestMethod.GET) public ModelAndView getlogsDownload(@RequestParam("src") String src, HttpSession session) { ModelAndView modelAndView = new ModelAndView(); String judge = judgeLogs(session); //判断是否登陆 if (!StringUtils.isEmpty(judge)) { modelAndView.setViewName("admin/logs"); modelAndView.addObject("errors", "Please log in first!"); return modelAndView; } try { //返回上一层 if ("upperStory".equals(src)) { String path = session.getAttribute("src").toString(); File dir = new File(path); src = dir.getParent(); } File dir = new File(src); session.setAttribute("src", src); if (dir.isDirectory()) { modelAndView.addObject("pathSrc", src); modelAndView.addObject("srcList", dir.list()); } modelAndView.setViewName("logs/logsDownload"); } catch (Exception e) { modelAndView.addObject("errors", e); modelAndView.setViewName("error/error"); } return modelAndView; }//检查是否是文件夹还是文件 @RequestMapping(value = "/checkDownload") public ModelAndView chackDownload(@RequestParam("filename") String filename, HttpSession session) throws Exception { ModelAndView modelAndView = new ModelAndView(); String judge = judgeLogs(session); //判断是否登陆 if (!StringUtils.isEmpty(judge)) { modelAndView.setViewName("admin/logs"); modelAndView.addObject("errors", "Please log in first!"); return modelAndView; } String path = session.getAttribute("src").toString(); File file = new File(path +File.separator+ filename); try { //测试此抽象路径名表示的文件是否是一个目录 if (file.isDirectory()) { modelAndView.setViewName("redirect:/downloads?src=" + path +File.separator+ filename); } else { session.setAttribute("src", path); modelAndView.setViewName("redirect:/download?filename=" + filename); } } catch (Exception e) { modelAndView.addObject("errors", e); modelAndView.setViewName("error/error"); } return modelAndView; } //下载 @RequestMapping(value = "/download") public ResponseEntity<byte[]> download(@RequestParam("filename") String filename, HttpSession session) throws Exception { //下载文件路径 //判断是否登陆 String judge = judgeLogs(session); if (!StringUtils.isEmpty(judge)) { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("admin/logs"); modelAndView.addObject("errors", "Please log in first!"); return null; } HttpHeaders headers = new HttpHeaders(); String path = session.getAttribute("src").toString(); File file = new File(path + File.separator + filename); try { //下载显示的文件名,解决中文名称乱码问题 String downloadFielName = new String(filename.getBytes("UTF-8"), "iso-8859-1"); //通知浏览器以attachment(下载方式)打开图片 headers.setContentDispositionFormData("attachment", downloadFielName); //application/octet-stream : 二进制流数据(最常见的文件下载)。 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); } catch (Exception e) { } return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }3.jsp页面显示<body> <div class="container"> 输入目录 <form action="<%=request.getContextPath()%>/downloads" method="get" commandName="src" role="form" name="src"> <input type="text" class="form-control" id="src" name="src" placeholder="Enter src:" required/> <button type="submit" class="btn btn-sm btn-success">提交</button> </form> <h3>文件下载</h3> <div><a href="<%=request.getContextPath()%>/downloads?src=upperStory">上一层</a></div> <c:forEach items="${srcList}" var="src"> <div><a href="<%=request.getContextPath()%>/checkDownload?filename=${src}">${src}</a></div> </c:forEach> </div> </body>