Meaningless Drivel is fun!*
The moose likes JSP and the fly likes file download from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "file download from JSP" Watch "file download from JSP" New topic
Author

file download from JSP

aitex abex
Greenhorn

Joined: Jun 02, 2006
Posts: 20
i wanat to make a link in JSP to download a file from remote place, such as when we click on this link a windows download pan open and when we click on save it save in my hard disk.
sangeeta kalyani
Greenhorn

Joined: Jun 04, 2006
Posts: 2
Hi,

You can call a Servlet on the onclick event of ur link from jsp. U will be passing the required attributes to this link.

Ex :

call javascript on ur onclick event.





And ur servlet will be like this.

Get the filecontent in bytes from the specified fileid (or what machanism u use to fetch the file content) and the call the file dialog box like this.

byte[] fileArray= getUrContent(fileid);
String fileName = "file1.txt"; //somthing
String fileType = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
setContentTypeHelper(response,fileType,fileName);//see down

ServletOutputStream outStream = response.getOutputStream();
outStream.write(fileArray);
outStream.flush();
outStream.close();



private void setContentTypeHelper(HttpServletResponse response,String fileType,String fileName){
if (fileType.trim().equalsIgnoreCase("txt")) {
response.setContentType( "text/plain" );
} else if (fileType.trim().equalsIgnoreCase("htm")) {
response.setContentType( "text/html" );
} else if (fileType.trim().equalsIgnoreCase("html")) {
response.setContentType( "text/html" );
} else if (fileType.trim().equalsIgnoreCase("doc")) {
response.setContentType( "application/msword" );
} else if (fileType.trim().equalsIgnoreCase("xls")) {
response.setContentType( "application/vnd.ms-excel" );
} else if (fileType.trim().equalsIgnoreCase("pdf")) {
response.setContentType( "application/pdf" );
} else if (fileType.trim().equalsIgnoreCase("ppt")) {
response.setContentType( "application/ppt" );
} else if (fileType.trim().equalsIgnoreCase("gif")) {
response.setContentType( "image/gif" );
} else if (fileType.trim().equalsIgnoreCase("bmp")) {
response.setContentType( "image/bmp" );
} else if (fileType.trim().equalsIgnoreCase("jpe")) {
response.setContentType( "image/jpeg" );
} else if (fileType.trim().equalsIgnoreCase("jpg")) {
response.setContentType( "image/jpeg" );
} else if (fileType.trim().equalsIgnoreCase("jpeg")) {
response.setContentType( "image/jpeg" );
} else if (fileType.trim().equalsIgnoreCase("rtf")) {
response.setContentType( "application/rtf" );
} else if (fileType.trim().equalsIgnoreCase("tiff")) {
response.setContentType( "image/tiff" );
} else if (fileType.trim().equalsIgnoreCase("tif")) {
response.setContentType( "image/tiff" );
} else {
response.setContentType( "application/octet-stream" );
}
response.setHeader("Content-Disposition","inline; filename=\""+fileName+"\"");
}

Note : u need to enter the servlet entry in ur web.xml.

Sangeeta
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42374
    
  64
What's wrong with

?
[ June 12, 2006: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61457
    
  67

Note : u need to enter the servlet entry in ur web.xml.


sangeeta, JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

Please read this for more information.

thanks,
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file download from JSP