About Me

My photo
India
Hey there, lovely people! I'm Hemant Menaria, and I'm passionate about programming. Having completed my MCA in 2011, I've delved into the world of coding with fervor. I believe in sharing knowledge, making complex concepts easy to grasp for everyone. JAVA, PHP, and ANDROID hold a special place in my heart, and I spend most of my time immersed in them. Currently, I'm deeply engaged in API/Webservice frameworks and crafting Hybrid mobile applications to enhance flexibility in the digital realm. If you ever find yourself stuck with a programming challenge, feel free to reach out to me at +91-8955499900 or drop me a line at hemantmenaria008@gmail.com. I'm always eager to help fellow enthusiasts navigate the intricacies of coding!

Tuesday, January 7, 2014

Download a file in jsp


<%@ 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