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