0

I'm new in jsp development

I'm trying to download a txt file.

My jsp code is the following

    <% String filepath="D:/Wok-Niva/Page/WebContent/UploadFile";
String filename="java.txt";



 try{
  response.setContentType("text/plain");   
  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");   

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

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


 }catch(Exception e)
 {
     out.print(e);
 }
%>

When I download the txt file, I get something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

 computer science

</body>
</html>

But the content of my text file is only "computer science"

RamChithra
  • 57
  • 2
  • 10
  • is that all the code? – Artur Peniche Oct 08 '15 at 10:57
  • Did you debug the value of `filename`? Is it `.txt` or `.html`? BTW if you open the html, browser will simply display text `computer science`. –  Oct 08 '15 at 11:17
  • 2
    You will need to create a servlet for file downloading, see [Implementing a simple file download servlet](https://stackoverflow.com/questions/1442893) – f_puras Oct 08 '15 at 11:23

0 Answers0