首页 > 解决方案 > 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[] 数据更新。

为什么这不适用于实际图像,但适用于一些简单的文本?

标签: javasqloraclegwt

解决方案


推荐阅读