ImageLob.rar解压后源码如下:
package com.sequoiadb.samples;
/******************************************************************************
*
* Name: ImageLob.java
* Description: This program demonstrates how to use the Java Driver to
* operate on large object in DB
* Get more details in API document
*
******************************************************************************/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bson.types.ObjectId;
import com.sequoiadb.base.CollectionSpace;
import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.DBLob;
import com.sequoiadb.base.Sequoiadb;
import com.sequoiadb.exception.BaseException;
public class ImageLob {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Please offer: ");
System.exit(0);
}
// the database server address
String connString = args[0];
String inputFileName = args[1];
String outputFileName = args[2];
Sequoiadb sdb = null;
CollectionSpace cs = null;
DBCollection cl = null;
// connect to database
try {
sdb = new Sequoiadb(connString, "", "");
} catch (BaseException e) {
System.out.println("Failed to connect to database: " + connString
+ ", error description " + e.getErrorType());
e.printStackTrace();
System.exit(1);
}
// get the table(collection)
if (sdb.isCollectionSpaceExist(Constants.CS_NAME))
cs = sdb.getCollectionSpace(Constants.CS_NAME);
else
cs = sdb.createCollectionSpace(Constants.CS_NAME);
if (cs.isCollectionExist(Constants.CL_NAME))
cl = cs.getCollection(Constants.CL_NAME);
else
cl = cs.createCollection(Constants.CL_NAME);
try {
System.out.println("going to write image " + inputFileName + " to database");
// read the image from local
byte[] data = readImageFromLocal(inputFileName);
// create a large object and than write the image into it
DBLob lob = cl.createLob();
lob.write(data);
lob.close();
ObjectId id = lob.getID();
System.out.println("finish writing image " + inputFileName + " to database");
System.out.println("going to read image from database");
// read the image from the specified large object
lob = cl.openLob(id);
int imageSize = (int)lob.getSize();
byte[] readData = new byte[imageSize];
@SuppressWarnings("unused")
int readLen = lob.read(readData);
lob.close();
// write the image to local
writeImageToLocal(readData, outputFileName);
System.out.println("finish writing image to local file " + outputFileName);
} catch(Exception e) {
e.printStackTrace();
}
sdb.disconnect();
}
public static byte[] readImageFromLocal(String inputFileName) throws IOException {
// open input file
File inputFile = new File(inputFileName);
InputStream in = new FileInputStream(inputFile);
// read data from input file
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len=in.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
// close input stream
in.close();
return outStream.toByteArray();
}
public static void writeImageToLocal(byte[] data, String outputFileName) throws IOException {
// open the output file
File outputFile = new File(outputFileName);
OutputStream out = new FileOutputStream(outputFile);
// write data to output file
out.write(data);
// close output steam
out.close();
}
}