0

Eclipse Juno、GWT、Java を使用しています。写真を MySQL データベースにアップロードしようとしています (GWTUpload SingleUpload を使用して画像を取得します)。以前の投稿 ( GWTUpload SingleUploader から写真を MySQL に保存する方法は? ) で、アップロードする前に画像をバイトに変換しようとしていました。その後、直接アップロードできると言われました。そこで、コードを変更し、GWT の推奨事項に従って、最終的に変数を「PreloadedImage photograph = new PreloadedImage();」として定義しました。ただし、ページが読み込まれなくなり、次のエラーが表示されます。

[より多くのコードを追加できるようにエラーとビューを削除]

関連するサーバー側のコード:

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;
}

}
4

1 に答える 1

1

シリアル化できない rpc 呼び出しでクラスを使用しようとしています。

への参照を削除PreloadedImageしますorg.AwardTracker.client.DBConnection

に関連:

I was trying to convert the image to byte prior to uploading

クライアント側でこれを行うことはできないため、画像をアップロードして、サーバー側で必要なことを行います。次に、Imageウィジェットを作成する UI から、または gwtupload のPreloadedImageヘルパーを使用して、元の画像または変更された画像を要求します。

注:HTML5を使用すると、ファイルシステムから画像を読み取る、キャンバスに書き込む、キャンバスで操作する、base64で読み取る、コンテンツをサーバーに送信するなど、クライアント側で特定のことを行うことができます。特定のブラウザでのみ動作します。

于 2013-06-25T07:13:19.567 に答える