打开文件

本文介绍了一个具体的文件下载Action实现过程,包括从系统参数中获取导出路径、设置响应头、读取文件并将其内容输出到客户端等关键步骤。同时提供了两种不同的实现方式,一种使用FileInputStream,另一种使用BufferedReader。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  public ActionForward openFile(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws IOException, ServletException
 {
  System.err.println("===com.eastpro.batch.action.BankBillingAction.openFile===begin===");

  String fileName=request.getParameter("fileName");
  String filePath="";

  try{
   response.setContentType("text/plain; charset=UTF-8");
   response.setHeader("Cache-Control", "no-cache");
   response.setHeader("Charset", "UTF-8");
   
   Function ff = new Function();
   Collection sys_result = ff.getSysParam();
   Data sys_data = (Data) sys_result.iterator().next();
   String exportPath = sys_data.getVcharProperty("export_path").trim();
   sys_data = null;
   sys_result = null;
   ff = null; 
   filePath=exportPath+fileName;
   OutputStream out=response.getOutputStream();
   File file=new File(filePath);
   InputStream is = new FileInputStream(file);
   byte temp[] = new byte[100 * 1024];
   int i=0;
   while((i=is.read(temp))!=-1){
    out.write(temp, 0, i);
   }
   out.flush();
   out.close();
   out=null;
   
  }catch (Exception e){
   System.err.println("Error: com.eastpro.batch.action.BankBillingAction.openFile()");
   e.printStackTrace();
  }
  System.err.println("===com.eastpro.batch.action.BankBillingAction.openFile===end===");

  return null;
 } 

 

 

 public ActionForward openFileOri(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws IOException, ServletException
 {
  System.err.println("===com.eastpro.batch.action.BankBillingAction.openFile===begin===");

  String fileName=request.getParameter("fileName");
  String filePath="";

  try{
   
   response.setContentType("text/plain; charset=UTF-8");
   response.setHeader("Cache-Control", "no-cache");
   response.setHeader("Charset", "UTF-8");
   
   Function ff = new Function();
   Collection sys_result = ff.getSysParam();
   Data sys_data = (Data) sys_result.iterator().next();
   String exportPath = sys_data.getVcharProperty("export_path").trim();
   sys_data = null;
   sys_result = null;
   ff = null; 
   filePath=exportPath+fileName;
   OutputStream out=response.getOutputStream();
   
   File file=new File(filePath);
   BufferedReader in=new BufferedReader(new FileReader(file));
   String strLine="";
   byte temp[] = new byte[100 * 1024];
   while((strLine=in.readLine())!=null){
    temp=strLine.getBytes();
    out.write(temp, 0, temp.length);
   }
   out.flush();
   out.close();
   out=null;
   
  }catch (Exception e){
   System.err.println("Error: com.eastpro.batch.action.BankBillingAction.openFile()");
   e.printStackTrace();
  }
  System.err.println("===com.eastpro.batch.action.BankBillingAction.openFile===end===");

  return null;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值