后台版的文件下载。前端,layui,后端,java、spring代码

时间:2019-03-14
本文章向大家介绍后台版的文件下载。前端,layui,后端,java、spring代码,主要包括后台版的文件下载。前端,layui,后端,java、spring代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

 js代码

  {field: 'contractFile', title: '合同文件', width: 120, rowspan: 2, align: 'center', templet: function (d) {
                    if (d.contractFile.length != 0) {
                        return '<a class="abc" href="#" lay-event="download">下载文件</a>'
                    } else {
                        return '- -'
                    }
                }},

鼠标点击触发事件

else if (obj.event === "download") {
            downloadTem(obj.data.contractFile)

调用事件

function downloadTem(pathData) {

        var url = window.location.href;
        var urlString = url.split("?");

        $('#download2').attr("action", "/lease/downloadFile?filePath=" + encodeURI(pathData) + "&" + urlString[1]);
        $('#download3').click();
    };

前端,采用隐藏域表单的方式。

  <form  action="" method="post" id="download2" style="display: none;">
            <input name="filename" value="" >
            <input type="submit" id="download3">
        </form>

后台对应的controller,但其实这截代码,有需要修改的地方。

 /**
     * 文件下载  todo 该功能需要进行修整,比如先进行查询搜索,如果有资源存在直接下载,并且在后面给改成公用的。
     * ,没有资源存在就弹窗进行提示。
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/downloadFile")
    public void download(@RequestParam(name = "filePath") String filePathParam, @RequestParam(name = "menuCode") String menuCodeParam, HttpServletResponse resp) throws Exception {
        //解码操作
        String filePath = URLDecoder.decode(filePathParam, "UTF-8");
        String[] files = filePath.split("\\\\");
        String directorys = files[0];
        String fileName = files[1];
        DataInputStream in = null;
        OutputStream out = null;
        resp.reset();

        if (fileName != null) {
            try {
                //获取项目的绝对路径
                String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
                String path2 = path.substring(1, path.length());
                String[] path3 = path2.split("/");
                String resultFileName = path2 + File.separator + "static" + File.separator + directorys + File.separator + fileName;
                InputStream inputStreamParam = new FileInputStream(resultFileName);
                resultFileName = URLEncoder.encode(resultFileName, "UTF-8");
                resp.setCharacterEncoding("UTF-8");
                resp.setHeader("Content-disposition", "attachment;filename=" + fileName);
                resp.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                in = new DataInputStream(inputStreamParam);
                out = resp.getOutputStream();
                int bytes = 0;
                byte[] bufferOut = new byte[1024];
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("错误---路径有了,但是硬盘中,没有对应的文件", e.getStackTrace());
                resp.reset();//清空输出流
                resp.setContentType("text/html;charset=UTF-8");
                resp.getWriter().print("<script type='text/javascript'>alert('文件不存在');location.href='/lease/list?menuCode=" + menuCodeParam + "'</script>");
                resp.getWriter().close();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                        logger.error("io错误", ioe.getStackTrace());
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException ioe2) {
                        ioe2.printStackTrace();
                        logger.error("io错误", ioe2.getStackTrace());
                    }
                }
            }
        } else {
            /*这个地方估计不会走,因为前端没有对应空路径的下载链接。*/
            resp.reset();
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().print("<script type='text/javascript'>alert('文件不存在');location.href='/lease/list?menuCode=" + menuCodeParam + "'</script>");
            resp.getWriter().close();
        }
    }

 

逻辑上,应该是点击后,先根据名字查找数据库看有没有存在的值。没有,直接返回,信息。有才进行下载。

所以,js方面应该是触发两次才对。