Link to home
Start Free TrialLog in
Avatar of javapollo
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)
Avatar of bloodredsun
bloodredsun
Flag of Australia image

I think you need to use a mixture of both as you can't create a binarystream to an http client.

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.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And how about this for writing the blobs to the DB?
      
            DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@your_server:1521:your_db","user","password");
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO BlobTest VALUES( ?, ? )" );
            pstmt.setString( 1, "myFilename");
            File uploadedFile = new File("uploadedfile.dat");
            InputStream is = new FileInputStream(uploadedFile );
            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(yourMaxMemorySize);
upload.setSizeMax(yourMaxRequestSize);
upload.setRepositoryPath(yourTempDirectory);

// Parse the request
List items = upload.parseRequest(request);
// 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();
}
Avatar of javapollo
javapollo

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...
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(request);".

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)