Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Facing problem with posting Excel file for download - Content in browser

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Facing problem with posting Excel file for download - Content in browser

    I am trying to post an Excel file to download from a servlet to a JSP. The content is written properly but to the IE browser and not in an Excel. I even tried giving the ContentType as Word/CSV etc. But nothing works, the content gets displayed only in the browser.

    PLEASE HELP.

    Code snippet in calling JSP:

    DownloadServlet downServlet = new DownloadServlet();
    downServlet.download(response);

    Code in Servlet:

    public class DownloadServlet extends HttpServlet{

    public void download(HttpServletResponse response) throws IOException {
    /PrintWriter out = response.getWriter( );
    response.setContentType("text/html");
    out.println("<H1>Hello from a Servlet</h2>"); /

    /HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");/


    String strData = "This is a Test for Download";
    byte[] b = strData.getBytes();

    //response.reset();

    response.setHeader("Content-disposition", "attachment; filename=" + "Download.xls");
    response.setContentType( "application/msword" );
    //response.addHeader( "Content-Description", "Download Excel file" );

    ServletOutputStream out = response.getOutputStream();
    out.write(b);

    //out.flush();
    out.close();
    }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Facing problem with posting Excel file for download - Content in browser

    set the content type like this and try..
    response.setContentType("application/vnd.ms-excel")
    and uncomment this line
    out.flush();

Similar Threads

  1. How to Download a file via FTP
    By JavaPF in forum Java Networking Tutorials
    Replies: 1
    Last Post: December 24th, 2011, 11:56 AM
  2. quite small (make the changelog non-static, i.e. load its content from a file)
    By Abdallah in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 30th, 2009, 09:00 PM
  3. Download a file to client from server
    By mvittalreddy in forum Java Networking
    Replies: 4
    Last Post: October 5th, 2009, 04:23 AM
  4. Detecting File Download Completion
    By fedfan in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 5th, 2009, 04:27 AM

Tags for this Thread