Jakarta Commons FileUpload - Files only partially stored to BLOB

D

Dundonald

I'm hoping some in here will have had experience with the Jakarta
commons file upload utility and inparticular writing an uploaded file
to a BLOB field.

I configure file upload as follows:

DiskFileUpload upload = new DiskFileUpload();
upload.setSizeMax(-1);
upload.setSizeThreshold(4096);
upload.setRepositoryPath("c:/temp");

Any file greater than 4096 bytes will be temporarily stored in to
c:/temp whilst the FileItem object from an uploaded file exists. This
can be verified by using the isInMemory() method on FileItem. It
returns false if a temporary file is stored.

I do see files created in c:/temp and the temporary file is the full
and correct file size. So let's say for example I upload an image at
400k I do see a .tmp file in c:/temp at 400k and if I rename the .tmp
file to .jpg and open it up it is displayed correctly.

However, when I retrieve the file that was written to a Blob field in
MySQL, and this can be any file, it is always a maximum of 65,535 bytes
(or there abouts). When I open this retrieved file, using the image as
an example, only a portion of the jpg is displayed (65,535 bytes
worth).

I was wondering if there was anything significant about this and more
importantly how to overcome this problem.

Any file I upload greater than 65,535 bytes has a problem. Any file
less than this in size works fine.

I've isolated the problem to the write to database rather than the
retrieve because I checked the database size before and after a test
uploading a 400k file. The database grew by only 65,535 (ish) bytes.

I can post the code I wrote to write a FileItem to a MySQL BLOB field
if it helps but I first wanted to test the water to see if this is a
known problem. I did google but there was no history of this problem
in the archive. In fact I'll post it anyway at the foot of this post.

Fingers crossed.



*** CODE TO WRITE A FileItem TO BLOB FIELD ****
* Note for background a record has already been created and this code
* uses the inserted record ID to update the Blob field of that record.
* A FileItem object called file is passed in to the method.
***

String sql = "";

try
{
/*
* Updated attached document record with file into blob
field.
*/
sql = "update "+<table name>+" "+
"set "+<blob field name>+"=? "+
"where "+<record id field
name>+"='"+lastInsertedID+"'";

Connection connection = getDatabaseConnection();

PreparedStatement preparedStatement =
connection.prepareStatement(sql);

/* paramater 1, because there is only 1 ? above. */
preparedStatement.setBinaryStream(1,
file.getInputStream(), (int)file.getSize());

preparedStatement.execute();
preparedStatement.close();
}
catch (SQLException e)
{
DatabaseQueryException dbqe = new
DatabaseQueryException(CLASS_NAME, "insertAttachedDocument", sql + " "
, e.getMessage());
throw dbqe;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top