admin管理员组

文章数量:1535106

其实这篇文章是上篇 pdf文件生成 的后续,当时开发的时候就直接下载到桌面了,然后上环境发
现下载到了服务器上,因此就赶紧搞了这个,网上的代码千篇一律,他们都是有后台,但
是缺少前台页面的方法,我也是看了不少才补全,亲测可用。

前端页面

<td>                
    <button type="button" id="btn_query" class="btn btn-primary pull-right" 
        onclick="download('${credit.score_card_uuid}')">下载
    </button>
</td>
    var excel_param;
    excel_param = {
        'start_time' : $("#start_time").val(),
        'end_time' : $("#end_time").val(),
        'order_uuids' : order_uuids
    };

    function download() {
        postDownLoadFile({
            url : '/ces/ces.do',
            data : excel_param,
            method : 'post'
        });
        $('#excel_modal').modal('hide');
    }

    var postDownLoadFile = function(options) {
        var config = $.extend(true, {
            method : 'post'
        }, options);
        var $iframe = $('<iframe id="down-file-iframe" />');
        var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
        $form.attr('action', config.url);
        for ( var key in config.data) {
            $form
                    .append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
        }
        $iframe.append($form);
        $(document.body).append($iframe);
        $form[0].submit();
        $iframe.remove();
    }

在页面 点击下载就会向后台发起请求


public void exportExcel(InstalmentAuthOrderForm form) {
//请求参数 用form接受
//接受参数然后查询数据,在服务器生成pdf文件

假设已经在服务器生成了pdf

String filename="xxx授信额度2018年08月22日11时30分24秒.pdf"
String path="D:\\pdf\\";
//执行下列方法
download(path+filename,response,filename);
    public void download(String filePath, HttpServletResponse response, String fname) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setHeader("Pragma", "No-Cache");
        response.setHeader("Cache-Control", "No-Cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("application/msexcel; charset=UTF-8");
        response.setHeader("Content-disposition","attachment; filename=" + URLEncoder.encode(fname, "UTF-8"));// 设定输出文件头
        ServletOutputStream out = null;
        FileInputStream in = new FileInputStream(filePath); // 读入文件
        out = response.getOutputStream();
        out.flush();
        int aRead = 0;
        while ((aRead = in.read()) != -1 & in != null) {
            out.write(aRead);
        }
        out.flush();
        in.close();
        out.close();
        return;
    }
}

效果图

下载完 可以用java执行个脚本 将服务器上的 文件删了,

https://blog.csdn/xuxie13/article/details/76463956

本文标签: 后端器上浏览器文件Java