Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
| Search | Today's Posts | Mark Forums Read
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 18th, 2005, 08:50 AM
Registered User
 
Join Date: Apr 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Download - file from Database [Blob field] -JSP

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,fileNam e.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 (permalink)  
Old June 29th, 2005, 06:46 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!
I had the same problem and I solved it adding as response header:
response.setHeader("cache-control", "must-revalidate");
I read in a office support page that msoffice attachments need to be stored in the pc to be opened, so I tried to use cache... and it worked.:)


  #3 (permalink)  
Old June 29th, 2005, 02:10 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: , , .
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that post it will come in handy for a project I have in mind later this year.

  #4 (permalink)  
Old August 18th, 2006, 07:55 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

> response.setHeader("cache-control", "must-revalidate");

This has just solved a problem I have been bashing my head against for the last two days. Writing out .csv files and .xls files using Spring by extending AbstractView and AbstractExcelView classes respectively.

When loading http://...../feedData.csv from shell or explorer it gave "Internet Explorer cannot download". Opening same URL in Excel worked.

This is on Office XP Professional and Explorer 6.0.2800....

Thanks to Pitust!!!!!!!

Tony J
  #5 (permalink)  
Old November 1st, 2006, 06:33 AM
Registered User
 
Join Date: Oct 2006
Location: KolKATA, , India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i did what u have said ...to display a blob image from jsp..the option of download or open is coming...but when i open it,it shows the blank page ..so please help.

Thanks in advance..
 here my code is like..

ResultSet rs=st.executeQuery(SqlQuery);
        System.out.println("PDFServlet.java: after execution of query");
        rs.next();

        String fileName="logo_large.gif";

        response.setContentType("image/gif");
        response.setHeader("Content-Disposition","attachment; filename=\""+fileName+"\"");
        response.setHeader("cache-control", "must-revalidate");


        ostr = response.getOutputStream();

        Blob bl = rs.getBlob("invoice_blob");
        buff = bl.getBytes(1, (int)bl.length());
        ostr.write(buff);
                ostr.flush();






  #6 (permalink)  
Old November 5th, 2006, 07:45 AM
Registered User
 
Join Date: Nov 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also look at microsoft for Office reader plugins.
(but only works on IE... sorry.)


  #7 (permalink)  
Old July 6th, 2009, 02:46 AM
Registered User
 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can i get the code for uploading a file to database(db2 if possible)... thanx in advance
  #8 (permalink)  
Old September 5th, 2009, 05:09 PM
Registered User
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I am getting junk when i m using this code.

Dear All,

I am getting junk when I am using this code.


Pls correct.

My code is as follows:
==========

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page session="true" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="org.apache.commons.fileupload.servlet.Serv letFileUpload"%>
<%@ page import="org.apache.commons.fileupload.disk.DiskFil eItemFactory"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ include file="connection.inc"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>saveattachments</title>
</head>
<body>
<center><table border="2" width="100%">
<tr><td><h1>Your files uploaded </h1></td></tr>
<%
String sql = "";
String fileName = "";
try
{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
//String filestring = item.getString();
if (item.isFormField()) {
} else {
try {
fileName = item.getName();
if (fileName!=null)
{
int fileSize = (int)item.getSize();
int attachid = 0;
sql = "select max(attach_id) aid from attachments";
ngrs = ngstmt.executeQuery(sql);
if (ngrs.next())
{
attachid = ngrs.getInt("aid")+1;
}
else
{
attachid = 1;
}
out.println(attachid);
InputStream iStream = item.getInputStream();

sql = " INSERT INTO ATTACHMENTS (ATTACH_ID,FILENO,ATTACHED_FILE,FILENAME) VALUES (?,?,?,?) ";
ngstmt3 = ngcon.prepareStatement(sql);
ngstmt3.setInt(1,attachid);//set docid
ngstmt3.setInt(2,1);//set Case File Number
ngstmt3.setBinaryStream(3,iStream,fileSize);
ngstmt3.setString(4,fileName);//set the filename
ngstmt3.execute();
ngcon.commit();
out.println("done");
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
}
%>
</table>
</center>
Reading file.
<%
try
{
byte[] fileBytes = new byte[5000000];
sql = "select * from attachments where attach_id=1";
ngrs = ngstmt.executeQuery(sql);
if (ngrs.next())
{
/* Code to retrieve from Blob field */
fileBytes = ngrs.getBytes("attached_file");
fileName = ngrs.getString("filename");
}


/* Code for download */

//String fileName = brsPlanDocument.getFileName();
String fileType = fileName.substring(fileName.indexOf(".")+1,fileNam e.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");
response.setHeader("cache-control", "must-revalidate");

ServletOutputStream outs = response.getOutputStream();
outs.write(fileBytes);
outs.flush();
outs.close();
}

catch (Exception e) {
e.printStackTrace();
}
}
catch(Exception e)
{
out.println("here"+e.getMessage());
e.printStackTrace();
}
finally
{
if(ngrs!=null)
ngrs.close();

%>
<%@ include file="connectionclose.inc" %>
<%
}
%>

</body>
</html>

Please mail me to sheeja_aj@yahoo.com

With Regards,

Sheeja Anil
  #9 (permalink)  
Old October 5th, 2009, 02:27 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Can you try by splitting your jsp into two parts, like one servlet which does insert and forwards to another servlet/jsp which reads from db and displays the content.
__________________
- Rakesh
http://iam-rakesh.blogspot.com
  #10 (permalink)  
Old January 7th, 2010, 03:04 AM
Registered User
 
Join Date: Jan 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regarding Blob download

Hi,
For downloading, I can access that dialog box for open/Save.But after that when i open the file, the file content is not correct format (Encrypted).


Can anyone help me to resove this....
its urgent..............




Similar Threads
Thread Thread Starter Forum Replies Last Post
BLOB: file is empty after retrieving blob data thr taraprasad Pro JSP 0 May 22nd, 2008 10:02 AM
Download file -JSP gagsy Pro JSP 0 February 28th, 2008 06:08 AM
BLOB from jsp upen Java Databases 0 October 30th, 2006 09:35 AM
upload a xls file into database blob column gummadis Pro VB Databases 0 September 14th, 2004 12:12 PM
BLOB field file types kend SQL Server ASP 0 December 19th, 2003 02:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.