当前位置:编程学习 > JAVA >>

jsp脚本下载文件遇到的问题

我用的FineReport官方的jsp代码,其中修改了一点自已的,用的是tomcat5.5 
<%@page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%><%   
      String filename = (String) request.getParameter("filename"); //获取参数filename,此处的参数名需要与下面超级链

接设置中传递的参数名相同 
      String filepath = (String) request.getParameter("filepath"); 
      response.reset();   
      response.setContentType("application/x-download");   
      String downloadfile = filepath+filename;    //设置下载文件的路径及名称 
      response.setHeader("Content-disposition","attachment; filename="+filename);  //设置下载文件的名称  
      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;   
          }   
      }   
%>  

这样,下载下来内容始终为空,但参数确实传递成功,filename传递前在模板值为 FineReport帮助文档.txt   filepath参数传递前

在模板值为 E:/文件/测试/帮助/     我试了好多种方法,只有写成 String downloadfile ="E:/文件/测试/帮助/  "+"FineReport

帮助文档.txt " 才能下载成功有内容,写成 String downloadfile =filepath+"FineReport帮助文档.txt " 或 String 

downloadfile ="E:/文件/测试/帮助/  "+filename  始终下载下来都是失败的,求各位大神指点, 是什么原因呢 --------------------编程问答-------------------- 你最好能跟一下 downloadfile 的值是不是对的 --------------------编程问答-------------------- 百度xml设置文件下载属性设置 --------------------编程问答--------------------
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,