java - oracle db 使用 GWT 在数据库中存储图像 blob
问题描述
我正在使用上传 GWT 上传图像,然后尝试将其存储到数据库中。客户端选择图像,然后将其传递给该类试图插入数据库的 http servlet。
它不会用图像更新表格列,但我创建了一个带有一些文本的测试字节数组,它将进入列。
表格列是
当我创建列时,我没有指定大小。
我不会发布客户端代码,因为我认为它在这里没有帮助。随着字节数组进入 servlet 就好了。
这是servlet类
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long maxFileSize = 20480;
long sizeInBytes = 0;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
String entityId = request.getParameter("entityId");
int id = Integer.parseInt(entityId);
try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//handling a normal form-field
if(item.isFormField()) {
System.out.println("Got a form field");
String name = item.getFieldName();
String value = item.getString();
System.out.print("Name:"+name+",Value:"+value);
} else {
//handling file loads
System.out.println("Not form field");
String fieldName = item.getFieldName();
String fileName = item.getName();
if (fileName != null) {
fileName = FilenameUtils.getName(fileName);
}
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
sizeInBytes = item.getSize();
byte[] data = item.get();
API.GetAccountManager().saveLogo(id, data);
数据库在本地,我在开发环境中。我能够将图像写入 tomcat 文件夹,因此我知道字节数组是正确的。图像大小为 10kb。数据字节数组大小为 10705
调试我看到图像首先存储在tomcat的临时文件夹中。
这是具有调用方法的类。
public void saveLogo(final int accountID, byte[] data){
Statement stmt = null;
Connection connection;
int rowCount;
PreparedStatement pstmt = null;
try {
connection = API.GetConnection();
pstmt = connection.prepareStatement(
"UPDATE account SET LOGO = ? WHERE account.id = ?");
byte[] testBytes = "this is a test".getBytes();
// pstmt.setBytes(1, data);
pstmt.setBinaryStream(1,new ByteArrayInputStream(data),data.length);
// pstmt.setBinaryStream(1,new ByteArrayInputStream(testBytes),testBytes.length);
pstmt.setInt(2, 5);
rowCount = pstmt.executeUpdate();
pstmt.execute();
connection.commit();
}catch (Exception exception) {
exception.printStackTrace();
if (exception instanceof DatabaseException) {
throw (DatabaseException) exception;
}
throw new DatabaseException("There was a problem executing a database call.", exception);
} finally {
if (pstmt != null) {
// pstmt.close();
}
}
}
这门课还需要一些工作。我可以插入 testBytes,但 0 rowCount 会用 byte[] 数据更新。
为什么这不适用于实际图像,但适用于一些简单的文本?
解决方案
推荐阅读
- python - 如何获得 DF 的单个值
- python - Python 无法解析嵌套参数
- python - 从列表 np.select 分配条件并创建一个新列(熊猫)
- reactjs - 如何在 AWS AppSync Client React App 中离线时禁用自动网络请求突变?
- java - 在 Java 中,动作复制到剪贴板可在所有平台上移植
- python - Mypy “Union[str, Dict[str, str]]”的索引类型“str”无效;预期类型“Union [int, slice]”
- mysql - 常用词过滤
- javascript - 尝试单击子项时侧边栏子菜单折叠
- api - Vertex AI 内部服务器错误:从 UI 测试模型预测时出现错误 500
- php - 如何在 unpkg 上使用 UMD 集成 ReactRangeslider