Discussions

Web tier: servlets, JSP, Web frameworks: Download - file from Database [Blob field] -JSP

  1. I am trying to write an application which can upload and download files [Excel, Word etc.] to/from an Oracle 9i database Blob field. I am using Java/JSP for the same.

    The upload part works just fine. However, when I try to download the file that I uploaded, I get an error.

    A dialog box comes up asking me to Open/Save the file. However, when I try to save it, it says

    “Internet Explorer cannot download …..tion=download&planId= testplan from localhost

    Internet Explorer was not able to open this Internet Site. The requested site is either unavailable or cannot be found. Please try again later.”


    I am using IE 6.0. I tested the same with Firefox browser and was able to download the file.

    Can anyone help?

    Code:

    Following is the code I am using for the same.




    /* Code to retrieve from Blob field */

    String sqlString = "SELECT PLAN_DOCUMENT_NAME,PLAN_DOCUMENT FROM BRS_PLAN_DESCRIPTION WHERE PLAN_ID = ?";

    ps = con.prepareStatement(sqlString);
    ps.setString (1,planId);
    rs = ps.executeQuery();
    while (rs.next()) {
    fileBytes = rs.getBytes("PLAN_DOCUMENT");
    fileName = rs.getString("PLAN_DOCUMENT_NAME");
    }

    brsPlanDocument.setPlanId(planId);
    brsPlanDocument.setFileName(fileName);
    brsPlanDocument.setFileBytes(fileBytes);



    /* Code for download */

    String fileName = brsPlanDocument.getFileName();
    String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());


    if (fileType.trim().equalsIgnoreCase("txt"))
    {
    response.setContentType( "text/plain" );
    }
    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
    {
    response.setContentType( "application/octet-stream" );
    }

    response.setHeader("Content-Disposition","attachment; filename=\""+fileName+"\"");
    response.setHeader("cache-control", "no-cache");

    byte[] fileBytes=brsPlanDocument.getFileBytes();

    ServletOutputStream outs = response.getOutputStream();
    outs.write(fileBytes);
    outs.flush();
    outs.close();
  2. Try this

    java.sql.Blob blobColumn = rs.getBlob(("PLAN_DOCUMENT");

    fileBytes = blobColumn.getBytes(0,(int)blobColumn.length());

    Baiju
  3. Hi,
     I'm facing similar kind of problem can any one please help me out.
      I'm uploading file as BLOB in database its working fine,

    Now I have to download file as BLOB and write to the browser as a Stream.

      Can any one help me on this please.

    Regards
    Krishna
  4. See for example DB taglib in Coldtags suite:
    http://www.servletsuite.com/jsp.htm
  5. Wat brsPlanDocument????[ Go to top ]

    what is this... please specify..
  6. I am facing the similar issue. Did you find solutions for this problem
  7. Is this problem solved? I am facing same problem.