我正在尝试从MySQL表中读取图像该图像被定义为longblob,我正在使用Eclipse Juno(更新),Java和GWT(最新版本)。但是,当我运行时,我在页面上收到错误消息(我的第一个视图没有显示):
onModuleLoad() threw an exception
Exception while loading module org.AwardTracker.client.AwardTracker. See Development Mode for details.
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: Deferred binding failed for 'org.AwardTracker.client.DBConnection' (did you forget to inherit a required module?)
at com.google.gwt.dev.shell.GWTBridgeImpl.create(GWTBridgeImpl.java:53)
at com.google.gwt.core.shared.GWT.create(GWT.java:57)
at com.google.gwt.core.client.GWT.create(GWT.java:85)
at org.AwardTracker.client.LoginView.<init>(LoginView.java:29)
at org.AwardTracker.client.AwardTracker.onModuleLoad(AwardTracker.java:14)
... 9 more
Caused by: com.google.gwt.core.ext.UnableToCompleteException: (see previous log entries)
at com.google.gwt.dev.shell.ModuleSpace.rebind(ModuleSpace.java:605)
at com.google.gwt.dev.shell.ModuleSpace.rebindAndCreate(ModuleSpace.java:465)
at com.google.gwt.dev.shell.GWTBridgeImpl.create(GWTBridgeImpl.java:49)
... 13 more
开发控制台有以下消息(前几行):
[DEBUG] [org.AwardTracker.AwardTracker] - Validating units:
[INFO] [org.AwardTracker.AwardTracker] - Ignored 4 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
[ERROR] [org.AwardTracker.AwardTracker] - Errors in 'file:/C:/Users/Glyndwr/Eclipse/Workspace/AwardTracker/src/org/AwardTracker/client/DBConnection.java'
[ERROR] [org.AwardTracker.AwardTracker] - Line 15: No source code is available for type java.sql.Blob; did you forget to inherit a required module?
我的DBConnection是:
package org.AwardTracker.client;
import java.util.List;
import java.sql.Blob;
import com.google.gwt.user.client.rpc.RemoteService;
/**
* The client side stub for the RPC service.
*/
public interface DBConnection extends RemoteService {
public User authenticateUser(String user, String pass, String level, String archived);
public User duplicateUser(String user, String pass, String level, String archived);
public User createUser(String user, String pass, String level, String archived);
public List<YouthMember> getYM(String id, String surname, String first_name, String dob, Blob image, String archived);
}
由此我认为java.sql.Blob不能在GWT中使用。我该怎么用?
我将图像存储在数据库中,而不是链接到图像,因为我只有少量的低分辨率图像需要存储,所以数据库不应该超过税收。
此致
格林
我做了以下更改:
在MySQLConnection类的服务器端,我添加了以下方法(但它有列出的错误):
public String getImageData(String id){
ResultSet result = null;
PreparedStatement ps = null;
String imageDataString = 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()) {
File file = new File(result.getString(1));
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
//Convert Image byte array into Base64 String
imageDataString = encodeImage(imageData);
imageInFile.close();
}
}
catch (FileNotFoundException e) {
System.out.println("getImageData - Image not found" + e);
}
catch (IOException ioe) {
System.out.println("getImageData - Exception while reading the Image " + ioe);
}
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 Base64.encodeBase64URLSafeString(imageByteArray); [This line has the error: Multiple markers at this line
- The method encodeBase64URLSafeString(byte[]) is undefined for the
type Base64
- Line breakpoint:MySQLConnection [line: 287] - encodeImage(byte[]). However I have import org.apache.commons.codec.binary.Base64;]
}
在客户端,我有:
public void renderYouthMemberTable(List<YouthMember> youthMemberList) {
if (youthMemberList != null) {
int row = 0;
int col = 0;
flexTable.clear();
for (YouthMember youthMember : youthMemberList) {
String id = youthMember.getId();
String imageDataString = getImageData(); [ Error: The method getImageData() is undefined for the type SelectPersonView]
// Converting a Base64 String into Image byte array
byte[] imageByteArray = decodeImage(imageDataString);
// Write a image byte array into file system
FileOutputStream imageOutFile = new FileOutputStream(
"/Users/jeeva/Pictures/wallpapers/water-drop-after-convert.jpg");
imageOutFile.write(imageByteArray);
flexTable.setWidget(row, col, new Label(imageByteArray)); [Error: The constructor Label(byte[]) is undefined]
//flexTable.setWidget(row, col, new Label(youthMember.getId()));
flexTable.setWidget(row + 1, col, new Label(youthMember.getSurname() + ", " + youthMember.getFirstname()));
//imageOutFile.close();
col = col + 1;
if (col > 7) {
row = row + 2;
col = 0;
}
}
}
}
/**
* Decodes the base64 string into byte array
* @param imageDataString - a {@link java.lang.String}
* @return byte array
*/
public static byte[] decodeImage(String imageDataString) {
return Base64.decodeBase64(imageDataString); [ With the error: The method decodeBase64(byte[]) in the type Base64 is not applicable for the arguments (String)]
}
我复制了以下代码:http://www.myjeeva.com/2012/07/how-to-convert-image-to-string-and-string-to-image-in-java/
此致
格林
答案 0 :(得分:0)
当您使用GWT时,请不要忘记您的最终代码是javascript。所以有些课程不可用。
可是:
- &GT; GWT中的图像可以有源URL的servlet URL(带参数)。
- &GT;这里描述的其他方式:GWT - image from database:使用返回base64字符串的服务rpc +回调时的图像初始化。
希望对你的问题很有帮助。