<%@ page import="java.util.*,java.io.*"%>
<%
File f = new File ("filePahtNeedToDownload"); //read file name.
/* set content type ( word,excel,pdf,txt..etc) here we try for a pdf file */
response.setContentType ("application/pdf");
//setting header and the Name by which user will save the file.
response.setHeader ("Content-Disposition", "attachment; filename=\"DownloadfileName.pdf\"");
//retrive file name
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>
No comments:
Post a Comment