I have the following on my jsp. The code worked fine until I tried to use it in a new html design page.
<code>
//page name is index.jsp
try //DISPLAY THE CONTENTS OF THE DATA DIRECTORY
{
File dirname = new File(PATH); // create an instance of the File class
if (dirname.exists()&&dirname.isDirectory())//check to see if File class dirname exists and is valid
{
String [] allfiles = dirname.list();//create an array of files in the dirname File class
for (int i=0; i< allfiles.length; i+=2)//loop through allfiles[] and print out file links
{
out.println("<br><table border='1' cellspacing='1' width='99%'>");
out.println("<tr><td width='50%' class='pageFont'><input type='checkbox' name='cb' value='"+allfiles[i]+"'>"+allfiles[i]+" ");
%>
<a class="a" href="index.jsp?downfile=C:\\data\\<%=allfiles[i+1]%>">DOWNLOAD</a></td>
<% if(i+1 < allfiles.length)//PRINTS OUT THE SECOND TD SO THAT WE HAVE 2 COLUMNS OF LINKS
{
out.println("<td width='50%' class='pageFont'><input type='checkbox' name='cb' value='"+allfiles[i+1]+"'>"+allfiles[i+1]+" ");
%>
<a class="a" href="index.jsp?downfile=C:\\data\\<%=allfiles[i+1]%>">DOWNLOAD</a></td></tr>
<%
}
out.println("</form></font></table>");
}
}
}
catch (IOException excep)
{
out.println("An IO exception has occured.");
}
</code>
Then when clicked this code is run:
<code>
try{
//CHECK TO SEE IF THE FILE HAS BEEN CLICKED TO DOWNLOAD SINGLE FILE
if (request.getParameter("downfile") != null)
{
String filePath = request.getParameter("downfile");
File f = new File(filePath);//CREATE AN INSTANCE OF THE FILE CLASS AND POINT IT TO THE LOCATION OF THE DIRECTORY CONTAINING THE FILES
if (f.exists() && f.canRead())
{
response.setContentType ("application/octet-stream");
response.setHeader ("Content-Disposition", "attachment;filename=\""+f.getName()+"\"");
response.setContentLength((int) f.length());
BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(f));
int i;
out.clearBuffer();
while ((i = fileInputStream.read()) != -1) out.write(i);
fileInputStream.close();
out.flush();
}
}
</code>
When I click on this link I get the download dialog box. If I open it I get the following open up in notepad(the files I am trying to give download links are .txt files)
Below is what is displayed in notepad ALL on 1 line:
<html>
<head>
<LINK rel="stylesheet" ty
That is displayed in all of the links that I click on. It is the first few lines of html code for index.jsp.
I know this code is probably not a good way of doing what I need but I got it to work fine until the change.
I am sure there is an easier way to code the download link without resubmitting the page.
Thanks in advance!!