javapollo
asked on
How to UPLOAD client side file to Server side DB as BLOB --URGENT--
Hi there,
I’m dealing with my junior project and I need to handle file upload-download issue urgently. First of all I use IBM WebSphere, DB2 UDB and JAKARTA STRUTS environment for JSP.
Steps are :
1) “Client side” selects file to upload from file dialog box in the form.
2) After submitting the actual path, Action class should read the binary file from the client side and write the file as BLOB field to DB2 database.
3) After inserting the file to DB, the client request for downloading the previous BLOB file must be handled. The one I mention is which we see on browsers “Open with”, “save to disk”.
I’ve made a research of course, Jakarta commons fileupload library is the one that I found but I additionally want to read/write blob fields, not as a plain file in server side.
An extra question is, suppose that I managed to insert blob file, I think I need to store the file’s name and extension to reassemble it, right ?
I’m waiting for your any type of suggestions, sample codes, beneficial document resources, etc. Of course the optimum code will catch the 500 points :))
The sample code may have a structure like that :
UploadAction Class Side :
Getting the binaryStream code for the specified file path from client side
Reading the binary stream from client side code
Writing the binary stream to DB as BLOB code
DownloadAction Class Side :
Getting the binary stream for the corresponding file ID from DB code (DownloadAction?fileID=xxx )
Reading the binary stream and sending it to client side (which the client is shown a classic “save as” “open with” browser dialog)
I’m dealing with my junior project and I need to handle file upload-download issue urgently. First of all I use IBM WebSphere, DB2 UDB and JAKARTA STRUTS environment for JSP.
Steps are :
1) “Client side” selects file to upload from file dialog box in the form.
2) After submitting the actual path, Action class should read the binary file from the client side and write the file as BLOB field to DB2 database.
3) After inserting the file to DB, the client request for downloading the previous BLOB file must be handled. The one I mention is which we see on browsers “Open with”, “save to disk”.
I’ve made a research of course, Jakarta commons fileupload library is the one that I found but I additionally want to read/write blob fields, not as a plain file in server side.
An extra question is, suppose that I managed to insert blob file, I think I need to store the file’s name and extension to reassemble it, right ?
I’m waiting for your any type of suggestions, sample codes, beneficial document resources, etc. Of course the optimum code will catch the 500 points :))
The sample code may have a structure like that :
UploadAction Class Side :
Getting the binaryStream code for the specified file path from client side
Reading the binary stream from client side code
Writing the binary stream to DB as BLOB code
DownloadAction Class Side :
Getting the binary stream for the corresponding file ID from DB code (DownloadAction?fileID=xxx
Reading the binary stream and sending it to client side (which the client is shown a classic “save as” “open with” browser dialog)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And how about this for writing the blobs to the DB?
DriverManager.registerDriv er( new oracle.jdbc.driver.OracleD river());
Connection conn = DriverManager.getConnectio n("jdbc:or acle:thin: @your_serv er:1521:yo ur_db","us er","passw ord");
PreparedStatement pstmt = conn.prepareStatement("INS ERT INTO BlobTest VALUES( ?, ? )" );
pstmt.setString( 1, "myFilename");
File uploadedFile = new File("uploadedfile.dat");
InputStream is = new FileInputStream(uploadedFi le );
pstmt.setBinaryStream( 2, is, (int)(uploadedFile.length( )));
pstmt.executeUpdate();
DriverManager.registerDriv
Connection conn = DriverManager.getConnectio
PreparedStatement pstmt = conn.prepareStatement("INS
pstmt.setString( 1, "myFilename");
File uploadedFile = new File("uploadedfile.dat");
InputStream is = new FileInputStream(uploadedFi
pstmt.setBinaryStream( 2, is, (int)(uploadedFile.length(
pstmt.executeUpdate();
And here is an example of upload code (using commons upload) that doesn't create a temporary file
Using jakarta commons upload in your action
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeThreshold(yo urMaxMemor ySize);
upload.setSizeMax(yourMaxR equestSize );
upload.setRepositoryPath(y ourTempDir ectory);
// Parse the request
List items = upload.parseRequest(reques t);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//we get an input stream of the file that can be used to input into the blob field replacing the need for a temporary file
InputStream uploadedStream = item.getInputStream();
//or as a byte array
//byte[] data = item.get();
}
Using jakarta commons upload in your action
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeThreshold(yo
upload.setSizeMax(yourMaxR
upload.setRepositoryPath(y
// Parse the request
List items = upload.parseRequest(reques
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//we get an input stream of the file that can be used to input into the blob field replacing the need for a temporary file
InputStream uploadedStream = item.getInputStream();
//or as a byte array
//byte[] data = item.get();
}
ASKER
Thanks, I got the picture now. I would be grateful if you could give me a sample code for handling the file-upload. I have a documentation about it : http://jakarta.apache.org/commons/fileupload/using.html
but it mentions "List of files" . What is it ? I want to upload one file per page. So does this mean that I need only the first index of this list ?
Thanks again bloodredsun...
but it mentions "List of files" . What is it ? I want to upload one file per page. So does this mean that I need only the first index of this list ?
Thanks again bloodredsun...
The code in the post directly above your contains the relavant code for handling uploads. It mentions "lists of files" as the method parseRequest( HttpServletRequest req) returns a List , q.v. "List items = upload.parseRequest(reques t);".
All you need to do is iterate over it (as shown) and get the first one which will be your file, then just stick it in the blob in the DB.
All you need to do is iterate over it (as shown) and get the first one which will be your file, then just stick it in the blob in the DB.
All you need to so is change the code to from
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
to
if (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
As this will get you the first one only (and only if it exists, so you avoid NullPointerExceptions)
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
to
if (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
As this will get you the first one only (and only if it exists, so you avoid NullPointerExceptions)
Use commons upload to upload to a set area. Then read the input file as an inputstream and input via a prepared statement or stored proc into the database. Then make sure you delete the file once upload is successful
For download, read from the database and get your blob, set the contentType in the response as whatever you file type you have and then write out to the response.