日期:2014-05-16  浏览次数:20501 次

Spring Sample里面的ImageDB问题
看到Spring文件夹里面的ImageDB应用例子,就想跑起来看看效果怎样。
如果使用Mysql数据库,修改一下DDL里面的content字段为LONGBLOB就可以很方便地将图片存储在MySql数据库中。
(MySQL的四种BLOB类型
类型  大小(单位:字节)
TinyBlob  最大 255B
Blob  最大 65K
MediumBlob  最大 16M
LongBlob  最大 4G

在Oracle数据库(我用的是Oracle 11g XE)中却不是成功保存图片,一点击上传应用就卡住了,在Eclipse的高度窗口下,出现在很多Daemon进程。(一时找不出原因,我想可能是Oracle数据库版本以及Spring2.5对BLOB的一些兼容性上的问题。非常欢迎高人指正这个猜测)
于是我修改了一下存储图片的方法为如下,就可正常地在Oracle中存储图片了。
@Transactional
	public void storeImage(final String name, final InputStream contentStream,
			final int contentLength, final String description)
			throws DataAccessException {
		if (lobHandler instanceof OracleLobHandler) {
			try {

				PreparedStatement ptst = getConnection()
						.prepareStatement(
								"INSERT INTO IMAGEDB VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())");
				ptst.setString(1, name);
				ptst.executeUpdate();
				// Execute SQL statement
				ptst = getConnection()
						.prepareStatement(
								"SELECT CONTENT ,DESCRIPTION FROM IMAGEDB WHERE IMAGE_NAME = ? FOR UPDATE");
				ptst.setString(1, name);

				ResultSet rs = ptst.executeQuery();
				rs.next();
				Blob contentBlob = rs.getBlob(1);
				OutputStream blobOutputStream = ((BLOB) contentBlob)
						.getBinaryOutputStream();
				FileCopyUtils.copy(contentStream, blobOutputStream);

				Clob descClob = rs.getClob(2);
				Writer clobWriter = ((oracle.sql.CLOB) descClob)
						.getCharacterOutputStream();
				clobWriter.write(description);
				clobWriter.close();

				contentStream.close();
				blobOutputStream.close();
				getConnection().commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			getJdbcTemplate()
					.execute(
							"INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)",
							new AbstractLobCreatingPreparedStatementCallback(
									this.lobHandler) {
								protected void setValues(PreparedStatement ps,
										LobCreator lobCreator)
										throws SQLException {
									ps.setString(1, name);
									lobCreator.setBlobAsBinaryStream(ps, 2,
											contentStream, contentLength);
									lobCreator.setClobAsString(ps, 3,
											description);
								}
							});
		}
	}