springmcv文件下载

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值