如何将byte []保存为java.sql.Clob?

时间:2015-05-04 11:02:46

标签: java byte bytearray clob

我有一个22.0 的文件,我必须将它作为Clob保存在我的实体中。

如何将byte[]保存为byte[]

3 个答案:

答案 0 :(得分:1)

要从Oracle上的Clob获取byte[],您可以

private Clob createClob(byte[] data, Connection conn) {
    CLOB clob = null; 

    try { 
        clob = CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_SESSION);

        clob.open(CLOB.MODE_READWRITE);

        OutputStream out = (OutputStream) clob.setAsciiStream(0L);

        out.write(data);
        out.flush();
        out.close();
    }
    catch (Exception e) {
        logger.error("", e);
    }
    finally {
        try {
            if (clob != null && clob.isOpen()) clob.close();
        }
        catch (SQLException e) {
            logger.error("Unable to close CLOB", e);
        }
    }

    return clob;
}

创建一个Blob(二进制)几乎是一样的,只需用 blob OutputStream out = (OutputStream) blob.setBinaryStream(0L);替换 clob

答案 1 :(得分:0)

使用Apache commons-io

byte[] data = IOUtils.toByteArray(clob.getAsciiStream());

如果您需要特殊编码或遇到问题,请尝试:

byte[] data = IOUtils.toByteArray(clob.getCharacterStream(), "UTF-8");

答案 2 :(得分:0)

import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.AbstractWindowedCursor; import android.database.Cursor; import android.database.CursorWindow; import android.database.CursorWrapper; import android.database.MatrixCursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ExifInterface; import android.net.Uri; import android.provider.MediaStore; import android.support.v4.content.CursorLoader; import android.util.Log; import java.io.IOException; import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; public class PhotoProvider extends ContentProvider { // The URI Matcher used by this content provider. private static final UriMatcher sUriMatcher = buildUriMatcher(); static final int PHOTO = 100; static final int PHOTO_COMMENT = 101; static final int PHOTO_AUTHOR = 102; static UriMatcher buildUriMatcher() { final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final String authority = PhotoContract.CONTENT_AUTHORITY; // matches photo/<any number> meaning any photo ID matcher.addURI(authority, PhotoContract.PATH_PHOTO + "/#", PHOTO); // matches photo/<photo id>/comment/ (comment text in ContentValues) matcher.addURI(authority, PhotoContract.PATH_PHOTO + "/#/" + PhotoContract.PATH_COMMENT, PHOTO_COMMENT); // matches photo/<photo id>/author/ (author name in ContentValues) //matcher.addURI(authority, PhotoContract.PATH_PHOTO + "/#/" + PhotoContract.PATH_AUTHOR, PHOTO_AUTHOR); return matcher; } @Override public String getType(Uri uri) { // Use the Uri Matcher to determine what kind of URI this is. final int match = sUriMatcher.match(uri); // Note: We always return single row of data, so content-type is always "a dir" switch (match) { case PHOTO_COMMENT: return PhotoContract.PhotoEntry.CONTENT_TYPE; case PHOTO_AUTHOR: return PhotoContract.PhotoEntry.CONTENT_TYPE; case PHOTO: return PhotoContract.PhotoEntry.CONTENT_TYPE; default: throw new UnsupportedOperationException("Unknown uri: " + uri); } } @Override public boolean onCreate() { return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { MatrixCursor retCursor = new MatrixCursor(projection); // open the specified image through the MediaStore to get base columns // then open image file through ExifInterface to get detail columns Long IMAGE_ID = PhotoContract.PhotoEntry.getImageIdFromUri(uri); Uri baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; baseUri = Uri.withAppendedPath(baseUri, ""+ IMAGE_ID); // http://androidsnippets.com/get-file-path-of-gallery-image // run query against MediaStore, projection = null means "get all fields" Cursor c = getContext().getContentResolver().query(baseUri, null, null, null, null); if (!c.moveToFirst()) { return null; } // match returned fields against projection, and copy into row[] Object[] row = new Object[projection.length]; int i = 0; /* // Cursor.getType() Requires API level > 10... for (String colName : projection) { int idx = c.getColumnIndex(colName); if (idx <= 0) return null; // ERROR int colType = c.getType(idx); switch (colType) { case Cursor.FIELD_TYPE_INTEGER: { row[i++] = c.getLong(idx); break; } case Cursor.FIELD_TYPE_FLOAT: { row[i++] = c.getFloat(idx); break; } case Cursor.FIELD_TYPE_STRING: { row[i++] = c.getString(idx); break; } case Cursor.FIELD_TYPE_BLOB: { row[i++] = c.getBlob(idx); break; } } } */ //http://stackoverflow.com/questions/11658239/cursor-gettype-for-api-level-11 CursorWrapper cw = (CursorWrapper)c; Class<?> cursorWrapper = CursorWrapper.class; Field mCursor = null; try { mCursor = cursorWrapper.getDeclaredField("mCursor"); mCursor.setAccessible(true); AbstractWindowedCursor abstractWindowedCursor = (AbstractWindowedCursor)mCursor.get(cw); CursorWindow cursorWindow = abstractWindowedCursor.getWindow(); int pos = abstractWindowedCursor.getPosition(); // NB! Expect resulting cursor to contain data in same order as projection! for (String colName : projection) { int idx = c.getColumnIndex(colName); // simple solution: If column name NOT FOUND in MediaStore, assume it's an EXIF tag // and skip if (idx >= 0) { if (cursorWindow.isNull(pos, idx)) { //Cursor.FIELD_TYPE_NULL row[i++] = null; } else if (cursorWindow.isLong(pos, idx)) { //Cursor.FIELD_TYPE_INTEGER row[i++] = c.getLong(idx); } else if (cursorWindow.isFloat(pos, idx)) { //Cursor.FIELD_TYPE_FLOAT row[i++] = c.getFloat(idx); } else if (cursorWindow.isString(pos, idx)) { //Cursor.FIELD_TYPE_STRING row[i++] = c.getString(idx); } else if (cursorWindow.isBlob(pos, idx)) { //Cursor.FIELD_TYPE_BLOB row[i++] = c.getBlob(idx); } } } } catch (Exception e) { e.printStackTrace(); } // have now handled the first i fields in projection. If there are any more, we expect // these to be valid EXIF tags. Should obviously make this more robust... try { ExifInterface exif = new ExifInterface((String) row[2]); while (i < projection.length) { row[i] = exif.getAttribute("UserComment"); //projection[i]); // String (or null) i++; } } catch (IOException e) { e.printStackTrace(); } retCursor.addRow(row); return retCursor; } @Override public Uri insert(Uri uri, ContentValues values) { return null; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // URI identifies IMAGE_ID and which EXIF tag to set; content://AUTH/photo/<image_id>/comment or /author // first, get IMAGE_ID and prepare URI (to get file path from MediaStore) Long IMAGE_ID = PhotoContract.PhotoEntry.getImageIdFromUri(uri); Uri baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; baseUri = Uri.withAppendedPath(baseUri, ""+ IMAGE_ID); // get DATA (path/filename) from MediaStore -- only need that specific piece of information String[] MS_projection = {MediaStore.Images.Media.DATA}; // http://androidsnippets.com/get-file-path-of-gallery-image Cursor c = getContext().getContentResolver().query(baseUri, MS_projection, null, null, null); if (!c.moveToFirst()) return -1; // can't get image path/filename... String thumbData = c.getString(0); // then, use URIMatcher to identify the "operation" (i.e., which tag to set) final int match = sUriMatcher.match(uri); String EXIF_tag; switch (match) { case PHOTO_COMMENT: { EXIF_tag = "UserComment"; break; } case PHOTO_AUTHOR: { EXIF_tag = "Author"; break; } default: throw new UnsupportedOperationException("Unknown uri: " + uri); } if (EXIF_tag == null) return -1; // finally, set tag in image try { ExifInterface exif = new ExifInterface(thumbData); String textToSet = values.get(EXIF_tag).toString(); if (textToSet == null) throw new IOException("Can't retrieve text to set from ContentValues, on key = " + EXIF_tag); if (textToSet.length() > 48) throw new IOException("Data too long (" + textToSet.length() + "), on key = " + EXIF_tag); exif.setAttribute(EXIF_tag, textToSet); exif.saveAttributes(); } catch (IOException e) { e.printStackTrace(); } return 1; // 1 image updated } } 可以接受char数组并创建一个可序列化的Clob对象。

你可以参考这里 http://docs.oracle.com/javase/7/docs/api/javax/sql/rowset/serial/SerialClob.html

javax.sql.rowset.serial.SerialClob可以接受字节数组

你可以参考这里 http://docs.oracle.com/javase/7/docs/api/javax/sql/rowset/serial/SerialBlob.html