How to create a Force Download link for pdf file in JSP
















Create 2 Files 

 DownloadPage.JSP code :-

<a href='downloadPdf.jsp?file=FILE.pdf' >Download PDF File</a>

downloadPdf.JSP code :-

<%@ page import="java.util.*,java.io.*"%>               

<%
 
  File f = new File ("E:/PDFfiles/Downloads/" + request.getParameter("file") );
  response.setContentType ("application/pdf");
  response.setHeader ("Content-Disposition", "attachment; filename="+request.getParameter("file"));
  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(); 
%>

2 comments:

Feel free to leave comment if you like above widget, have any questions or just say Hi! :)

How to Check if file or folder exist in JSP or Java
























<%
                    String path = "";
                    path = "D:\Folder\Data\2012\feb\Pan\PDml\001543\SomeFile.pdf";
                    File file=new File(path);
                    boolean exists = file.exists();
                    if (!exists) {
                   out.println("File not Available for Download");
                    }else{  // It returns true if File or directory exists
                   %>
                 
                   <a href=''><font class='blueList'>Download File</font> </a>
                 
                    <% }
                    %>


0 comments:

Feel free to leave comment if you like above widget, have any questions or just say Hi! :)

How to Force Format Pen-drive or Memory card using cmd or Command prompt in xp

press windows+R

type cmd in Run 

then paste below command using mouse

format G: /FS:FAT32 /x  

then Enter

0 comments:

Feel free to leave comment if you like above widget, have any questions or just say Hi! :)

Related Posts Plugin for WordPress, Blogger...