Saving binary data from SQL Server column

O

oswaldt

Hello,

Please let me know if this is not the proper group to post to, and if
this question has been answered in the past please forgive me, I
couldn't seem to find it.

I have a process that puts a file into a image column in SQL Server.
This runs very well and fast on a 2 megabyte file. Here is the code I
use.

CallableStatement call = mConnection.getCallableStatement("{call
spInsertWebFile (?,?,?,?,?,?,?,?,?,?)}");
call.setInt(index++, this.SiteNumber);
call.setInt(index++, this.SiteRevision);
call.setString(index++, sf.getDestinationFile());
call.setDate(index++, new java.sql.Date(f.lastModified()));
call.setLong(index++, f.length());
call.setString(index++, this.BatchRevisedBy);
call.setString(index++, mil.dla.dlis.wds.DateFormat.format(now));
call.setInt(index++, 0);
call.setBinaryStream(index++, (InputStream)new FileInputStream(f),
(int)f.length());

call.registerOutParameter(index, java.sql.Types.INTEGER);

call.execute();

I will say it uploads the file in about 10 seconds. Now when I
download I can't seem to find \such an efficient manner as this. I
have to use a loop to read some bytes from the OutputStream and then
write them to the FileInputStream. This method takes about 40 seconds.

byte[] input = new byte[blockSize];
int count=0;
int ret=0;
java.io.InputStream in = rs.getBinaryStream("file_data");
int size = rs.getInt("File_size");


String fullpath = path;
int off=0;

if (! System.getProperty("os.name").equalsIgnoreCase("windows"))
fullpath = fullpath.replace('\\','/');

java.io.File f = new java.io.File(fullpath);
if (f.exists()) f.delete();

java.io.FileOutputStream out = new
java.io.FileOutputStream(fullpath);

while (size > 0)
{
count++;
if (size < blockSize)
input = new byte[size];
ret = in.read(input);
out.write(input);
size -= ret;
}
out.close();

Is there a more efficient way? I have tied just reading the whole file
into a byte array but as you can imagine that uses a lot of memory. I
have also tried different block sizes like 128k, 256k and the default
of 64k.

thanks,
Tyson
 

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
474,001
Messages
2,570,251
Members
46,850
Latest member
Brightrs

Latest Threads

Top