Stored word doc as BLOB but will not download properly

N

Nickname

Hello:

I am trying to store word documents and PDFs file into an oracle
database. I am using a BLOB. I have been able to dump both word
documents and pdf files into a local flat file to ensure that they were
stored completely.

However the problem I have is this. When I try and download the file
via a JSP the word document does not work properly, but the PDF file
works fine. I have spent 2 days on this and cannot seem to solve the
mystery why it works for a pdf and not a word doc.

The pdf is 143 KB and the word doc is 18.5 KB.

Here is how I am executing my code. I have a JSP called TestResume.jsp
which creates an object of type Resume.java.
I then call the printResume method and I get a file dialog box. For a
PDF I can save the file or have it open properly. But for the word
document, MS word opens but asks me to do some file conversion. It asks
me to 'Select the encoding that makes your document readable'. Choices
are windows, ms-dos, other encoding.
<%@ page
import="jep.db.*,jep.JepResume,java.io_OutputStream,java.io.InputStream"
%>

<%
String fileName = "BobSmith_09_Mar_2005.doc";

JepResume jepresumedb = new JepResume();
try {
jepresumedb.printResume(fileName, response);
} catch (databaseException dbe1) {out.println(dbe1);}
%>
public void printResume(String fileName, HttpServletResponse res)
throws databaseException
{

DbUtil dbconn = new DbUtil();
dbconn.openConnection();

// tried both content types
//res.setContentType("application/vnd.ms-word");
res.setContentType("application/msword");
res.setHeader("Content-Disposition", "attachment; filename=" + "\"" +
fileName + "\"");

OutputStream os = null;
try {
os = res.getOutputStream();
} catch (IOException ioe0){ioe0.printStackTrace();}


PreparedStatement p = null;
try{
p = dbconn.getConnection().prepareStatement("select RESUME_BLOB
from RESUME where FILENAME='" + fileName + "'");
ResultSet rs = p.executeQuery();
os = res.getOutputStream();

while (rs.next()){
BufferedInputStream pdfData = new
BufferedInputStream(rs.getBinaryStream("RESUME_BLOB"));
byte[] buf = new byte[100 * 1024];
int len;
while ((len = pdfData.read(buf, 0, buf.length)) != -1)
{
os.write(buf, 0, len);
}
}
rs.close();
dbconn.endConnection();

} catch (Exception ee) {System.out.println("Failure in " +
ee.toString()); ee.printStackTrace();}

}
 
L

Lans Redmond

if you are storing it as a blob why not use the getBlob() from the
preparedStatement instead of using getBinaryStream?
Nickname said:
Hello:

I am trying to store word documents and PDFs file into an oracle
database. I am using a BLOB. I have been able to dump both word
documents and pdf files into a local flat file to ensure that they were
stored completely.

However the problem I have is this. When I try and download the file
via a JSP the word document does not work properly, but the PDF file
works fine. I have spent 2 days on this and cannot seem to solve the
mystery why it works for a pdf and not a word doc.

The pdf is 143 KB and the word doc is 18.5 KB.

Here is how I am executing my code. I have a JSP called TestResume.jsp
which creates an object of type Resume.java.
I then call the printResume method and I get a file dialog box. For a
PDF I can save the file or have it open properly. But for the word
document, MS word opens but asks me to do some file conversion. It asks
me to 'Select the encoding that makes your document readable'. Choices
are windows, ms-dos, other encoding.
<%@ page
import="jep.db.*,jep.JepResume,java.io_OutputStream,java.io.InputStream"
%>

<%
String fileName = "BobSmith_09_Mar_2005.doc";

JepResume jepresumedb = new JepResume();
try {
jepresumedb.printResume(fileName, response);
} catch (databaseException dbe1) {out.println(dbe1);}
%>
public void printResume(String fileName, HttpServletResponse res)
throws databaseException
{

DbUtil dbconn = new DbUtil();
dbconn.openConnection();

// tried both content types
//res.setContentType("application/vnd.ms-word");
res.setContentType("application/msword");
res.setHeader("Content-Disposition", "attachment; filename=" + "\"" +
fileName + "\"");

OutputStream os = null;
try {
os = res.getOutputStream();
} catch (IOException ioe0){ioe0.printStackTrace();}


PreparedStatement p = null;
try{
p = dbconn.getConnection().prepareStatement("select RESUME_BLOB
from RESUME where FILENAME='" + fileName + "'");
ResultSet rs = p.executeQuery();
os = res.getOutputStream();

while (rs.next()){
BufferedInputStream pdfData = new
BufferedInputStream(rs.getBinaryStream("RESUME_BLOB"));
byte[] buf = new byte[100 * 1024];
int len;
while ((len = pdfData.read(buf, 0, buf.length)) != -1)
{
os.write(buf, 0, len);
}
}
rs.close();
dbconn.endConnection();

} catch (Exception ee) {System.out.println("Failure in " +
ee.toString()); ee.printStackTrace();}

}
 
N

Nickname

Sorry for the long delay in replying, I was out of the office.

I have also tried using getBlob() but this also gives me the same
problem.
I dont think my problem is how I retrieve it from the database but
possibly how I am
presenting it to the browser. As mentioned PDF files work fine, word
documents do not.
 
M

Marcin Grunwald

Nickname said:
Hello:

I am trying to store word documents and PDFs file into an oracle
database. I am using a BLOB. I have been able to dump both word
documents and pdf files into a local flat file to ensure that they were
stored completely.

However the problem I have is this. When I try and download the file
via a JSP the word document does not work properly, but the PDF file
works fine. I have spent 2 days on this and cannot seem to solve the
mystery why it works for a pdf and not a word doc.

The pdf is 143 KB and the word doc is 18.5 KB.

Here is how I am executing my code. I have a JSP called TestResume.jsp
which creates an object of type Resume.java.
I then call the printResume method and I get a file dialog box. For a
PDF I can save the file or have it open properly. But for the word
document, MS word opens but asks me to do some file conversion. It asks
me to 'Select the encoding that makes your document readable'. Choices
are windows, ms-dos, other encoding.
I think that servlet would be better solution than JSP (in this case). It's
easier to make mistake in JSP code.
<%@ page
import="jep.db.*,jep.JepResume,java.io_OutputStream,java.io.InputStream"
%>

<%
String fileName = "BobSmith_09_Mar_2005.doc";
For example, after "page import" you send newline.


Check:
- size of file "BobSmith_09_Mar_2005.doc" on the server and downloaded one
- compare file "BobSmith_09_Mar_2005.doc" on the server and downloaded one
(check differences)
- download file "BobSmith_09_Mar_2005.doc" from server in usual way and
record headers send by server - compare them with headers send by your JSP.

Begin from sending file directly from filesystem (throw out that part with
database) and ofcourse unit test would be nice. If you have that part
working then try to get that file from database.
 
N

Nickname

Thanks.. I ended up putting it into a servlet.... This solved the
problem.

So I got it working after several more hours, unsure of the exact
cause.

Thanks for all the input.
 
M

Marcin Grunwald

Nickname said:
Thanks.. I ended up putting it into a servlet.... This solved the
problem.

So I got it working after several more hours, unsure of the exact
cause.

Thanks for all the input.

You had error in your JSP, I wrote about it. You sent newline before file
content:
<%@ page
import="jep.db.*,jep.JepResume,java.io_OutputStream,java.io.InputStream"
%>

<%
String fileName = "BobSmith_09_Mar_2005.doc";

According to this PDF and DOC files were corrupted and only your PDF viewer
could manage with it, but it was too much for the MS Word. I told you about
serlvet because I didn't know whether it was the only bug in your JSP and
as you see sending binary files is simpler with servlet than with JSP :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top