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.iutputStream,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();}
}
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.iutputStream,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();}
}