网页下载模板前台后台

前台

$(function(){
    $("tbody").on("click",".dyzs",function(){
        window.location.href=ctx+"/tzsb/DCDoc";
    });

});

当点击时跳转页面,不能用ajax,必须用action或链接。
action

@Action(value="DCDoc", results={@Result(type="json",params={"root","res"})})
        public void DCDoc() throws Exception{
            res=new ResultObject();
            HttpServletResponse response = ServletActionContext.getResponse();
            HttpServletRequest request = ServletActionContext.getRequest();
            String path = "D:\\wzgzpt\\web\\template";
            //String path2 = request.getSession().getServletContext().getRealPath(
            //        "/resources");
            //String path = "";
            String fileName = "气瓶使用登记证.doc";
            String filePath = path + "\\" + fileName;
            res.setMsg("ok");
            publicFunc.downLoadFile(filePath, response, fileName, "doc");
        }

downLoadFile方法

public static boolean downLoadFile(String filePath,
            HttpServletResponse response, String fileName, String fileType)
            throws Exception {
            File file = new File(filePath);  //根据文件路径获得File文件
            //设置文件类型(这样设置就不止是下Excel文件了,一举多得)
            if("pdf".equals(fileType)){
               response.setContentType("application/pdf;charset=GBK");
            }else if("xls".equals(fileType)){
               response.setContentType("application/msexcel;charset=GBK");
            }else if("doc".equals(fileType)){
               response.setContentType("application/msword;charset=GBK");
            }
            //文件名
            response.setHeader("Content-Disposition", "attachment;filename=\""
                + new String(fileName.getBytes(), "ISO8859-1") + "\"");
            response.setContentLength((int) file.length());
            byte[] buffer = new byte[4096];// 缓冲区
            BufferedOutputStream output = null;
            BufferedInputStream input = null;
            try {
              output = new BufferedOutputStream(response.getOutputStream());
              input = new BufferedInputStream(new FileInputStream(file));
              int n = -1;
              //遍历,开始下载
              while ((n = input.read(buffer, 0, 4096)) > -1) {
                 output.write(buffer, 0, n);
              }
              output.flush();   //不可少
              response.flushBuffer();//不可少
            } catch (Exception e) {
              //异常自己捕捉       
            } finally {
               //关闭流,不可少
               if (input != null)
                    input.close();
               if (output != null)
                    output.close();
            }
           return false;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值