jsp txt文件下载给了我jsp的内容

时间:2015-11-02 10:08:24

标签: java jsp

我有一个jsp项目,在我的项目中我想从该位置下载一个txt文件。代码确实有效,但文件的内容实际上是jsp索引页面内容,我不知道这是怎么回事。

    String filename = "Teste.txt";   
  String filepath = "D:\\Online Secure File Transfer System\\project";   
  response.setContentType("APPLICATION/OCTET-STREAM");   
  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   

  java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath + filename);  

  int i;   
  while ((i=fileInputStream.read()) != -1) {  
    out.write(i);   
  }   
  fileInputStream.close();  

1 个答案:

答案 0 :(得分:1)

我认为您的文件路径有问题,请不要附加字符串

  String filename = "Teste.txt"; 
  String filepath = "D:\\Online Secure File Transfer System\\project\\Teste.txt";   
  response.setContentType("APPLICATION/OCTET-STREAM");   
  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   
  java.io.FileInputStream fileInputStream=new java.io.FileInputStream(filepath);  

  int i;   
  while ((i=fileInputStream.read()) != -1) {  
    out.write(i);   
  }   
  fileInputStream.close();