• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

file download from JSP

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with

?
[ June 12, 2006: Message edited by: Ulf Dittmer ]
 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic