• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing and retrieving file in JSP?

 
Manjit Barman
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am working on an application where i need to upload a file and store that as a blob column in oracle database. And later on i need to download the same from database to the users machine.
Any input would be highly appreciated!

A snippet of code for your reference!

[CODE]
stmt.execute(" INSERT INTO blob_table (id, file_name) VALUES ("+intId+" ,empty_blob() )");
// Retrieve BLOB locator
strStatement = " SELECT file_name FROM blob_table WHERE id = "+intId+" FOR UPDATE";
resSet = stmt.executeQuery(strStatement);

if (resSet.next()) {
// Get the BLOB locator and open output stream for the BLOB
bFileName = ((oracle.jdbc.driver.OracleResultSet)resSet).getBLOB(1);
blobOutputStream = bFileName.getBinaryOutputStream();
// Open the sample file as a stream for insertion
// into the BLOB column

MultipartRequest multi =new MultipartRequest(request, real_path, 1000 * 1024,
new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());
Enumeration files = multi.getFileNames();
String name = (String)files.nextElement();
File file = multi.getFile(name);


inStream = new FileInputStream(file);
// Buffer to hold chunks of data to being written to the BLOB.
byte[] bBuffer = new byte[bFileName.getBufferSize()*100];
int intBytesRead = 0;

while ((intBytesRead = inStream.read(bBuffer)) != -1) {
// write to BLOB
blobOutputStream.write(bBuffer,0,intBytesRead);
}
// closing the streams and committing
inStream.close();
blobOutputStream.close();
conn.commit();
}

[CODE]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your question?
 
Manjit Barman
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that!
I can upload the file into database. Now, i need to retrieve the same from database into a file in the users machine. How can i do that?

Thanks a heap!
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a snippet from a servlet that streams a file to the user's browser.
Obviously, in your case, you'll get the input stream from the database blob, not a file.

Note: This really needs to be done from a servet -- not a JSP.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I second Ben's recommendation: JSP is a poor technology choice for streaming anything but text.
 
Manjit Barman
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million Ben!
Lets see if that works for me!
 
Manjit Barman
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben:
Here is my code snippet for reading the file from database blob.
But the file is not created in docroot?
Could you please tell me where i am making me fool?

[CODE]
//write to a file
InputStream blobInputStream = null;
OutputStream sampleFileStream1 = null;
// select the blob locator from the database
strStatement ="SELECT file_name FROM blob_table WHERE id = "+intId+" ";
stmt = conn.createStatement();
resSet = stmt.executeQuery(strStatement);
if (resSet.next()) {
//strFileName = resSet.getString("name");
// get the locator
bFileName = ((oracle.jdbc.driver.OracleResultSet)resSet).getBLOB(1);
}



// get the input stream
blobInputStream = bFileName.getBinaryStream();
// Open the file to write to

String serverPath=request.getRealPath("/");
File file2Save = new File(serverPath.concat("\\IncidentSummary.doc)"));
System.out.println("The File Name is: "+file2Save.getName());
blobOutputStream = new FileOutputStream(file2Save);
// get the buffer size to use
int intBufferSize = bFileName.getBufferSize()*100;
// Buffer to hold chunks of data to read from the BLOB.
byte[] bBuffer = new byte[intBufferSize];
// Read a chunk of data from the BLOB, and
// write the chunk to the file.
int intBytesRead = 0;
while ((intBytesRead = blobInputStream.read(bBuffer)) != -1) {
blobOutputStream.write(bBuffer,0,intBytesRead);
}
[CODE]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic