java - 尝试将定义为 PreloadedImage 的图像写入 MySQL 时出错

标签 java mysql eclipse gwt

我正在使用 Eclipse Juno、GWT、Java。 我正在尝试将照片上传到 MySQL 数据库(我使用 GWTUpload SingleUpload 获取图像)。在我之前的帖子(How to save a photograph to MySQL from GWTUpload SingleUploader?)中,我试图在上传之前将图像转换为字节。然后我就被告知可以直接上传。因此,我更改了代码并遵循 GWT 建议,最终将变量定义为“PreloadedImage Photograph = new PreloadedImage();”。但是,该页面不再加载,并且出现以下错误。

[删除错误和 View 以允许添加更多代码]

相关服务器端代码:

public YthMmbrSectDtls createYouthMember(String youthMemberId,
        String surname, String firstname, java.sql.Date dob,
        PreloadedImage photograph, java.sql.Date archived, String sectionDetailsId,
        String section, String pack, java.sql.Date startDate,
        java.sql.Date endDate) {

    YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(
          "INSERT INTO at_cub_details at_section_details" +
                  " (cd_surname, cd_first_name, cd_dob, cd_photograph, cd_archived," +
                  " sd_section, sd_pack, sd_start_date, sd_end_date) " +
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
      ps.setString(1, surname);
      ps.setString(2, firstname);
      ps.setBlob(3, (java.sql.Blob) photograph);
      ps.setDate(4, (java.sql.Date) dob);
      ps.setDate(5, (java.sql.Date) archived);
      ps.setString(6, section);
      ps.setString(7, pack);
      ps.setDate(8, (java.sql.Date) startDate);
      ps.setDate(9, (java.sql.Date) endDate);
      ps.executeUpdate();
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException createYouthMember 1.");
        e.printStackTrace();
        user = null;
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException createYouthMember 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException createYouthMember 3.");
                e.printStackTrace();
            }
        }
    }
return ythMmbrSectDtls;
}

DBConnection类

package org.AwardTracker.server;

import gwtupload.client.PreloadedImage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.server.Base64Utils;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import org.AwardTracker.client.BCrypt;
import org.AwardTracker.client.Base64Decode;
import org.AwardTracker.client.DBConnection;
import org.AwardTracker.client.SectionDetails;
import org.AwardTracker.client.User;
import org.AwardTracker.client.YouthMember;
import org.AwardTracker.client.YthMmbrSectDtls;
import org.AwardTracker.server.Base64Encode;
import org.AwardTracker.server.Base64Encode2;



public class MySQLConnection extends RemoteServiceServlet implements DBConnection {
private Connection conn = null;
private String status;
private String url = "jdbc:mysql://localhost:3306/awardtracker";
private String user = "ss";
private String pass = "ss";
public MySQLConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(url, user, pass);
    } catch (Exception e) {
        //NEVER catch exceptions like this

        System.out.println("Error connecting to database - not good eh");
        e.printStackTrace();
    }
}

public User authenticateUser(String userName, String pass, String level1, java.sql.Date archived1) {
    User user = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    String stored_hash = null;
    try {
        ps = conn.prepareStatement(
          "select * from at_accounts where acc_email_address = \"" + userName  + "\"");
      result = ps.executeQuery();
      while (result.next()) {
         user = new User(result.getString(1), result.getString(2), result.getString(3), null);
         stored_hash = result.getString(3);
      }
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException authenticateUser 1.");
        e.printStackTrace();
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException authenticateUser 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException authenticateUser 3.");
                e.printStackTrace();
            }
        }
    }
    if (BCrypt.checkpw(pass, stored_hash))  {
    } else {
        user = null;
    }
    return user;
}

public User duplicateUser(String userName, String pass, String level1, java.sql.Date archived1) {
    User user = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(
          "select * from at_accounts where acc_email_address = \"" + userName  + "\"");
      result = ps.executeQuery();
      while (result.next()) {
         user = new User(result.getString(1), null, null, null);
      }
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException duplicateUser 1.");
        e.printStackTrace();
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException duplicateUser 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException duplicateUser 3.");
                e.printStackTrace();
            }
        }
    }
return user;
}

public User createUser(String userName, String pass, String level1, java.sql.Date archived1) {
    User user = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    String pw_hash = BCrypt.hashpw(pass, BCrypt.gensalt());
    try {
      ps = conn.prepareStatement(
          "INSERT INTO at_accounts (acc_email_address, acc_password) " +
                  "VALUES (?, ?)");
      ps.setString(1, userName);
      ps.setString(2, pw_hash);
      ps.executeUpdate();
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException createUser 1.");
        e.printStackTrace();
        user = null;
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException createUser 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException createUser 3.");
                e.printStackTrace();
            }
        }
    }
return user;
}

public List<YouthMember> getYM(String id, String surname, String first_name, java.sql.Date dob, String photograph, java.sql.Date archived, String pack) {
    List<YouthMember> youthMemberList = new ArrayList<YouthMember>();
    //YouthMember youthMember = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    String imageString = null;

    try {
      ps = conn.prepareStatement(
      "SELECT at_cub_details.*" +
        " FROM at_cub_details, at_section_details" + 
        " WHERE (at_cub_details.cd_id = at_section_details.cd_id" +
            " AND at_section_details.sd_pack = \"" + pack + "\"" + ")");

      result = ps.executeQuery();
      while (result.next()) {
          imageString = getImageData(result.getString(1));
          YouthMember youthMember = new YouthMember(result.getString(1), result.getString(2), result.getString(3), result.getDate(4), imageString, result.getDate(6));
          youthMemberList.add(youthMember);
      }
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException getYouthMember 1.");
        e.printStackTrace();
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException getYouthMember 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException getYouthMember 3.");
                e.printStackTrace();
            }
        }
    }
return youthMemberList;
}

public String getImageData(String id){
    ResultSet result = null;
    PreparedStatement ps = null;
    String imageDataString = null;
    String base64 = null;
    try {
        // Read in the image from the database.
        ps = conn.prepareStatement(
              "SELECT at_cub_details.cd_photograph " +
                      "FROM at_cub_details " + 
                      "WHERE at_cub_details.cd_id = \"" + id + "\"");
        result = ps.executeQuery();
        while (result.next()) {
            java.sql.Blob imageBlob = result.getBlob(1);
            byte[] imageData = imageBlob.getBytes(1, (int) imageBlob.length());

            //Convert Image byte array into Base64 String
            imageDataString = encodeImage(imageData);
            imageDataString = "data:image/jpeg;base64,"+imageDataString;
        }

    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException getImageData 1.");
        e.printStackTrace();
        user = null;
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException getImageData 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException getImageData 3.");
                e.printStackTrace();
            }
        }
    }
    return imageDataString;
}
 /**
  * Encodes the byte array into base64 string
  * @param imageByteArray - byte array
  * @return String a {@link java.lang.String}
  */
public static String encodeImage(byte[] imageByteArray) {
    return Base64Encode2.encode(imageByteArray);
}

public YthMmbrSectDtls createYouthMember(String youthMemberId,
        String surname, String firstname, Date dob,
        String password, Date archived, String sectionDetailsId,
        String section, String pack, Date startDate, Date endDate) {

    YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    try {
      ps = conn.prepareStatement(
          "INSERT INTO at_cub_details at_section_details" +
                  " (cd_surname, cd_first_name, cd_dob, cd_archived," +
                  " sd_section, sd_pack, sd_start_date, sd_end_date) " +
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
      ps.setString(1, surname);
      ps.setString(2, firstname);
      ps.setDate(3, (java.sql.Date) dob);
      ps.setDate(4, (java.sql.Date) archived);
      ps.setString(5, section);
      ps.setString(6, pack);
      ps.setDate(7, (java.sql.Date) startDate);
      ps.setDate(8, (java.sql.Date) endDate);
      ps.executeUpdate();
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException createYouthMember 1.");
        e.printStackTrace();
        user = null;
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException createYouthMember 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException createYouthMember 3.");
                e.printStackTrace();
            }
        }
    }
return ythMmbrSectDtls;
}

public YthMmbrSectDtls updateYouthMember(String id, String surname,
        String firstname, java.sql.Date dob, java.sql.Date archived,
        String section, String pack, java.sql.Date startDate,
        java.sql.Date endDate) {
    YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;

    try {
      ps = conn.prepareStatement(
              "UPDATE at_cub_details at_section_details" +
                      " (cd_surname, cd_first_name, cd_dob, cd_archived," +
                      " sd_section, sd_pack, sd_start_date, sd_end_date) " +
                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
          ps.setString(1, surname);
          ps.setString(2, firstname);
          ps.setDate(3, (java.sql.Date) dob);
          ps.setDate(4, (java.sql.Date) archived);
          ps.setString(5, section);
          ps.setString(6, pack);
          ps.setDate(7, (java.sql.Date) startDate);
          ps.setDate(8, (java.sql.Date) endDate);
          ps.executeUpdate();
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException updateYouthMember 1.");
        e.printStackTrace();
        user = null;
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException updateYouthMember 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException updateYouthMember 3.");
                e.printStackTrace();
            }
        }
    }
return ythMmbrSectDtls;
}

public YouthMember readYM(String id, String surname, String first_name, java.sql.Date dob, String photograph, java.sql.Date archived) {
    YouthMember youthMemberDetails = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    String imageString = null;
    System.out.println("ID received = " + id);
    try {
        ps = conn.prepareStatement(
            "SELECT * " + 
            "FROM at_cub_details " +
            "WHERE at_cub_details.cd_id = \"" + id + "\"");
//          ps.getString(1, id);
//              "SELECT * " +
//              " FROM at_cub_details");
//              " FROM at_cub_details " + 
//              " WHERE at_cub_details.cd_id = 2");
//              " WHERE at_cub_details.cd_id = \"" + id + "\"");
      result = ps.executeQuery();

      while (result.next()) {
          imageString = getImageData(result.getString(1));
          youthMemberDetails = new YouthMember(result.getString(1), result.getString(2), result.getString(3), result.getDate(4), imageString, result.getDate(6));
      }
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException readYM 1.");
        e.printStackTrace();
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException readYM 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException readYM 3.");
                e.printStackTrace();
            }
        }
    }
    return youthMemberDetails;
}

public SectionDetails invested(String id, String youth_member_id,
        String section, String pack, java.sql.Date start_date,
        java.sql.Date end_date) {
    SectionDetails sectionDetails = null; // necessary unless you do something in the exception handler
    ResultSet result = null;
    PreparedStatement ps = null;
    try {
        ps = conn.prepareStatement(
          "SELECT MIN(start_date)" +
          "FROM at_section_details" +
          "WHERE cd_id = \"" + youth_member_id  + "\"" +
                "AND cd_pack = \"" + pack + "\"");
      result = ps.executeQuery();
      while (result.next()) {
         sectionDetails = new SectionDetails(null, null, null, null, result.getDate(5), null);
      }
    }
    catch (SQLException e) {
      //do stuff on fail
        System.out.println("SQLException invested 1.");
        e.printStackTrace();
    }
    finally {
        if (result != null) {
            try {
                result.close();
            }
            catch (SQLException e) {
                System.out.println("SQLException invested 2.");
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            }   
            catch (SQLException e) {
                System.out.println("SQLException invested 3.");
                e.printStackTrace();
            }
        }
    }
    return sectionDetails;
}

}

最佳答案

您正在尝试在 rpc 调用中使用不可序列化的类。

org.AwardTracker.client.DBConnection 中删除对 PreloadedImage 的引用。

相关:

我试图在上传之前将图像转换为字节

您无法在客户端执行此操作,因此请上传您的图像并在服务器端执行您想要的任何操作。然后从创建 Image 小部件或使用 gwtupload 的 PreloadedImage 帮助程序的 ui 中请求原始或修改后的图像。

注意:使用 HTML5,您可以在客户端执行某些操作,例如从文件系统读取图像、将其写入 Canvas 、在 Canvas 中操作它、以 Base64 读取图像并将内容发送到服务器等.但这仅适用于某些浏览器。

关于java - 尝试将定义为 PreloadedImage 的图像写入 MySQL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289854/

相关文章:

php - 在数据库中存储关键字的好方法是什么?

javascript - 我必须将我想要在 Eclipse 上的动态 Web 项目中使用的 xml 文件放在哪里?

java - 与 SVN 冲突后将项目重构为新项目后出现 NoInitialContextException

java - getView ListView 选中项颜色改变

java - 在 Android 上使用 iText 将 HTML 转换为 PDF。如何设置 UTF-8(变音符号)?

java - JSR-308 : Clarification on Nonnull, ParameterAreNonnullByDefault 以及 Eclipse Kepler 检测它的方式

eclipse - Gradle和Eclipse的Hibernate映射文件路径

java - WebDriver 无法解析为 Selenium 3.5.3 的类型

mysql - 在 Mysql 中标记旧的重复项

java - 如何在 JFreeChart 中显示过滤后的数据