oracle db通过GWT

时间:2018-12-20 15:55:30

标签: java sql oracle gwt

我正在使用上载GWT上载图像,然后尝试将其存储到数据库中。客户端选择图像,然后将其传递到该类试图插入数据库的http servlet。

它不会用图像更新表列,但是我创建了一个带有一些文本的测试字节数组,它将进入该列。

表格列是

enter image description here

创建列时,我没有指定大小。

我不会发布客户端代码,因为我认为这对您没有帮助。当字节数组进入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的temp文件夹中。

这是带有调用方法的类。

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 []数据更新。

为什么不能使用实际图像,但是可以使用一些简单的文本?

0 个答案:

没有答案