Saving binary data from SQL Server column

Discussion in 'Java' started by oswaldt@ameritech.net, Jul 12, 2006.

  1. Guest

    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
    , Jul 12, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Taylor
    Replies:
    2
    Views:
    3,732
    Ryan Taylor
    Oct 22, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,504
    Luis Esteban Valencia
    Jan 6, 2005
  3. Bhavesh
    Replies:
    0
    Views:
    421
    Bhavesh
    Jul 16, 2007
  4. Bhavesh
    Replies:
    5
    Views:
    626
    Bhavesh
    Jul 18, 2007
  5. Bhavesh
    Replies:
    1
    Views:
    449
    Bhavesh
    Jul 17, 2007
Loading...

Share This Page