| Author |
storing and retrieving file in JSP?
|
Manjit Barman
Greenhorn
Joined: Apr 04, 2005
Posts: 25
|
|
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
Joined: Jan 10, 2002
Posts: 56204
|
|
|
What's your question?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Manjit Barman
Greenhorn
Joined: Apr 04, 2005
Posts: 25
|
|
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
I second Ben's recommendation: JSP is a poor technology choice for streaming anything but text.
|
 |
Manjit Barman
Greenhorn
Joined: Apr 04, 2005
Posts: 25
|
|
Thanks a million Ben! Lets see if that works for me!
|
 |
Manjit Barman
Greenhorn
Joined: Apr 04, 2005
Posts: 25
|
|
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]
|
 |
 |
|
|
subject: storing and retrieving file in JSP?
|
|
|