数据库中的userPic的类型为Image,java中实体类的类型为Blob。将图片存入数据库的时候出现异常。
请路过此地的各位大侠拔刀相助。帮小弟解决困难,以下是相关代码。
if (user.getUserPic() != null) {
// 得到项目路径
String dirPath = System.getProperty("user.dir");
String s = dirPath.replace("bin", "");
// 用来存储照片的文件路径(完整路径)
String picPath = s + "webapps\\Dcu32X_BS\\avatar\\"
+ user.getId() + ".jpg";
// 创建目录
// CreateFileUtil.createDir(dirPath);
// 创建文件
CreateFileUtil.CreateFile(picPath);
// 远程访问路径
user.setPicPath("/avatar/" + user.getId() + ".jpg");
Blob photo = user.getUserPic();
InputStream in = null;
try {
in = photo.getBinaryStream();
} catch (SQLException e1) {
e1.printStackTrace();
}
// 用来存储照片数据的缓冲区
byte[] buf = new byte[1024];
FileOutputStream out;
try {
out = new FileOutputStream(picPath);// 写入图片数据
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }action中的方法 public String addUsers() throws Exception {
String rs = null;
if (f!=null) {
FileInputStream fis = new FileInputStream(getF());
Blob b = Hibernate.createBlob(fis);
System.out.println("bbbbbbbb"+b);
user.setUserPic(b);
System.out.println("图片:"+user.getUserPic());
System.out.println("姓名:"+user.getUserName());
}
boolean bool = userManageBiz.addUsers(user);
if (bool) {
rs = SUCCESS;
}else {
rs = ERROR;
}
return rs;
}报出的异常为:util.JDBCExceptionReporter - 数据类型 image 和 varbinary(max) 在 equal to 运算符中不兼容。
请路过此地的各位大侠拔刀相助。帮小弟解决困难,以下是相关代码。
if (user.getUserPic() != null) {
// 得到项目路径
String dirPath = System.getProperty("user.dir");
String s = dirPath.replace("bin", "");
// 用来存储照片的文件路径(完整路径)
String picPath = s + "webapps\\Dcu32X_BS\\avatar\\"
+ user.getId() + ".jpg";
// 创建目录
// CreateFileUtil.createDir(dirPath);
// 创建文件
CreateFileUtil.CreateFile(picPath);
// 远程访问路径
user.setPicPath("/avatar/" + user.getId() + ".jpg");
Blob photo = user.getUserPic();
InputStream in = null;
try {
in = photo.getBinaryStream();
} catch (SQLException e1) {
e1.printStackTrace();
}
// 用来存储照片数据的缓冲区
byte[] buf = new byte[1024];
FileOutputStream out;
try {
out = new FileOutputStream(picPath);// 写入图片数据
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }action中的方法 public String addUsers() throws Exception {
String rs = null;
if (f!=null) {
FileInputStream fis = new FileInputStream(getF());
Blob b = Hibernate.createBlob(fis);
System.out.println("bbbbbbbb"+b);
user.setUserPic(b);
System.out.println("图片:"+user.getUserPic());
System.out.println("姓名:"+user.getUserName());
}
boolean bool = userManageBiz.addUsers(user);
if (bool) {
rs = SUCCESS;
}else {
rs = ERROR;
}
return rs;
}报出的异常为:util.JDBCExceptionReporter - 数据类型 image 和 varbinary(max) 在 equal to 运算符中不兼容。
解决方案 »
- prefuse applet加到jsp上 InvocationTargetException
- 使用jsp上传文件,input file,无法获取绝对路径问题
- hibernate 奇怪的主键问题 是BUG吗?
- xloadtree reload() 两次
- SOS:sql语句太长,换行问题 高分
- 请问如何从jsp中传图片到applet中
- 求助关于 previous()函数的问题
- 敢问谁知道UEditor插件配置window.UEDITOR_HOME_URL
- 请问J2sdk,resin和oracle8.1.7 是否有冲突!如何解决???
- 就问怎么在servlet里添加resultset的代码使得查询得到的数据能在jsp中显示
- 想做一个系统,想问问大家,你会哪几个
- 发帖。求真理
没用spring吗
<column name="UserPic" />
</property>
user.setPicPath("/avatar/" + user.getId() + ".jpg");
Blob photo = user.getUserPic(); //那这个怎么转换呢?
InputStream in = null;
你要的是 InputStream 转化成 InputStream 网上找
byte[] data = new byte[(int) f.available()];
BufferedInputStream bis = new BufferedInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(
baos);k = 0;
while ((k = bis.read()) != -1) {
bos.write(k);
}
bos.flush(); // 提交文件流,很关键
bis.close();
user.setUserPic(baos.toByteArray());System.out.println("图片:"+user.getUserPic());
System.out.println("姓名:"+user.getUserName());
}
boolean bool = userManageBiz.addUsers(user);
if (bool) {
rs = SUCCESS;
}else {
rs = ERROR;
}
return rs;
}
byte[] data = new byte[(int) f.available()];
BufferedInputStream bis = new BufferedInputStream(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(
baos);k = 0;
while ((k = bis.read()) != -1) {
bos.write(k);
}
bos.flush(); // 提交文件流,很关键
bis.close();
user.setUserPic(baos.toByteArray());System.out.println("图片:"+user.getUserPic());
System.out.println("姓名:"+user.getUserName());
}
boolean bool = userManageBiz.addUsers(user);
if (bool) {
rs = SUCCESS;
}else {
rs = ERROR;
}
return rs;
}
前辈,这个data没有用到?
user.setPicPath("/avatar/" + user.getId() + ".jpg");
Blob photo = user.getUserPic();InputStream in = null;
try {
in = photo.getBinaryStream();} catch (SQLException e1) {
e1.printStackTrace();
}
// 用来存储照片数据的缓冲区
byte[] buf = new byte[1024];
FileOutputStream out;
这个地方也是用Byte?那下面调用byte的什么方法?请前辈指导