Uderc programming article aggregator sites

Creating a file download link on jsp

Tags:
Answers: Have 14 answers
Advertisement
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!!
Advertisement
The best answer: Well all was fine with this jsp until I moved it to ApacheJServ. Now the problem has resurfaced(although it is a little different now)
I had moved the following code to the top of my page:
//CHECK TO SEE IF EITHER DOWNFILE OR ZIPFILE VARIABLE EXIST, AND IF THEY DO SET CONTENT TYPE BEFORE SENDING ANY HTML CODE TO THE BROWSER
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();
                        response.flushBuffer();
catch (Exception e){}
//This is where the java code ends and the javascript/html code begins.Then further into the page I create the file download links like so:
                        <a class="a" href="main.jsp?downfile=C:\\data\\<%=allfiles[i+1]%>">DOWNLOAD</a>I know this isnt a secure way of doing this but I am on a intranet.
The problem I am having now is that when I click on one of the links and download the file and then open it, I get the contents of the file plus concatenated to the end of it is the first few HTML lines of the actual jsp that I downloaded the file from. Before I was just getting the first few lines of html from the jsp, not the actual contents of the downloaded file.
This is an example of what I am getting:
This is the contents of the file that I downloaded.//This is where the file contents ends.
<html>
<head>
<LINK rel="stylesheet" type="text/css" href="default.css">
<script language="JavaScript1.2">
//function that allows user to select all checkboxes
function doIt(v)
for(var i=0;i<document.form1.cb.length;i++)
   document.form1.cb.checked=eval(v);
function swap(imageName,image)
imageName.src = "templateImages/"+image;
function imageOver(imageSrc, imageName)
     changeImage = new Image();
     changeImage.src = imageSrc;
     document.images[imageName].src = changeImage.src;
var hide = true;
function hideShow( ) /
Any morre ideas?
TIA!
BTW, I went to ApacheJServ because they wont let me use tomcat :(