aspose file tools*
The moose likes JSP and the fly likes download link in jsp ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "download link in jsp ?" Watch "download link in jsp ?" New topic
Author

download link in jsp ?

manojdesai sompadi
Greenhorn

Joined: Apr 22, 2005
Posts: 10
i have to put a doanload link in a jsp page
<a href="/test/xyz.zip"></a>

but i have the xyz file in local drive c:\\download\xyz.zip
i am generating the file in local folder i can't put it in the server root.
how to map the download file from local drive to server path without copying the file into server path ?
thanks in advance
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You would need to write a servlet that sets the content-type, sets the content-disposition, and then streams the file to the browser.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
manojdesai sompadi
Greenhorn

Joined: Apr 22, 2005
Posts: 10
thanks for the response
fallowing code modified from the example you have reffered when i am running this servlet its opening a download dialog box asking for downloading the servlet.java file instead of java.zip file ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is your code even finding the file?

On line 4, you're saying: filename = "c:\\file.zip".
On line 17, you're saying: getResourceAsStream("WEB-INF" + filename));

This would mean that your looking for a file with a path and filename of:
"WEB-INFC:\\file.zip"

If the file is not inside your webapp, you would get the input stream with:
FileInputStream in = new FileInputStream(new File(filename));

"context.getResourceAsStream" is for files that are inside your webapp.
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
thanks for the answer .
its working now but the save dialogbox i am getting the servlet name as default name to save ,instead of it how to get the test.zip file name as deafult name .
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The content-disposition header is where you set the desired name for the file.
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
i will try it
thanks for your prompt responces.
i appreciate your help
thanks once again
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
i have set the name in Content-Disposition
as fallows
resp.setHeader(filename,"inline;filename=\""+filename +"\"");
by this i could get the file extenison correctly but still name of the file it takes as name of servlet.
advice
kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
i commented the line
resp.setHeader(filename,"inline;filename=\""+filename +"\"");
i am getting same result .
why application is not picking up setHeader ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

kamesh aru
Ranch Hand

Joined: Mar 16, 2002
Posts: 150
i changed the content type to
contentType = "application/x-zip";
resp.setHeader("Content-Disposition",
" inline; filename=" + filename);
then its working

thanks for the responces till now they are very helpful to me
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: download link in jsp ?