Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file download from JSP

 
aitex abex
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
sangeeta kalyani
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with

?
[ June 12, 2006: Message edited by: Ulf Dittmer ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic