在线报表FineReport中如何通过文件路径下载文件

2025-10-24 23:32:30

1、示例

假设已经存在如下图所示的模板文件:

下面我们要做的就是点击对应的文件名,弹出文件下载对话框,下载对应路径下的文件

在线报表FineReport中如何通过文件路径下载文件

2、自定义下载方式

假设所有的文件都存在D盘目录下,下面我们来看下完整的jsp代码:

 <%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%><%

      String filename = (String) request.getParameter("filename"); //获取参数filename,此处的参数名需要与下面超级链接设置中传递的参数名相同 

      filename = java.net.URLDecoder.decode(filename, "utf-8");

 response.reset();

      response.setContentType("application/x-download");

 String downloadfile = "D:/"+filename;    //设置下载文件的路径及名称

response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("utf-8"爷伐激),"iso8859-1")); //设置下载文件的名称

      java.io.OutputStream outp = null;

      java.io.FileInputStream in = null;

      try

      {

          outp = response.getOutputStream();

   歌胳       in = new java.io.FileInputStream(downloadfile); 

          byte[] b = new byte[1024];

          int i = 0;

          while((i = in.read(b)) > 0)

          {

              outp.write(b, 0, i);

          }

          outp.flush();

      }

      catch(Exception e)

      {

          System.out.println("Error!");

          e.printStackTrace();

      }

      finally

      {

          if(in != null)

          {

              in.close();

       畜脂       in = null;

          }

          if(outp != null)

          {

              outp.close();

              outp = null;

          }

      }

%>

将此文件命名为download.jsp并保存在Tomcat服务器下的某个应用下,此处我们将其保存在WebReport应用下。

3、超级链接设置

右击C3单元格选择超级链接,然后增加一个网页链接,链接的路径为:/WebReport/download.jsp,并增加一个参数,参数名为filename,值选择公式=C3,具体设置如下图:

在线报表FineReport中如何通过文件路径下载文件

4、效果查看

启动tomcat服务器,然后启动浏览器,然后访问如上报表,点击对应的超级链接就能弹出 下载对话框,如下效果图。

在线报表FineReport中如何通过文件路径下载文件

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢