`
leezpz
  • 浏览: 1320 次
  • 性别: Icon_minigender_1
  • 来自: 西安
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

流读图片并保存在DB,从盘符下载图片

    博客分类:
  • java
dao 
阅读更多
dao層書寫邏輯
public boolean savePic(String sql, String path, Picture picture) {
PreparedStatement pstmt = null;
if (path != null && path.trim() != "") {
try {
File file = new File(path);
InputStream in = new FileInputStream(file);
long len = file.length();
@SuppressWarnings("unused")
int leng = (int) len;
pstmt = dao.getConn().prepareStatement(sql);
pstmt.setBinaryStream(1, in, in.available());
pstmt.execute();
if (pstmt.execute()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}



从DB层取数据写出到指定的盘符下
@SuppressWarnings("unchecked")
public boolean getPic(String sql) {
OutputStream out = null;
int i = 0;
// Vector v = new Vector();
// String pathName = null;
try {
ResultSet rs = dao.getConn().createStatement().executeQuery(sql);
while (rs.next()) {
i++;
// pathName = "E:/x" + i + ".jpg";
InputStream in = rs.getBinaryStream(1);
int len = in.available();
byte[] bytes = new byte[len * 100];
while (in.read(bytes) != -1) {
out = new FileOutputStream("E:/x" + i + ".jpg");
out.write(bytes);
// v.add(pathName);
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

action类书写
private Pic p = new Pic();
private Picture picture = new Picture();
//要针对对象必须有get/set方法
HttpServletRequest request = null;

public String savPicture() {
String sql = "insert into picture values(?)";
System.out.println(getPicture().getPicMemo() + "=================");
if (p.savePic(sql, getPicture().getPicMemo(), getPicture())) {
System.out.println("照片保存成功");
return SUCCESS;
}
System.out.println("照片保存失败");
return ERROR;
}

public String getpic() {
String sql = "select * from picture";
if (p.getPic(sql)) {
System.out.println("showPicture成功!");
return SUCCESS;
}
return ERROR;
}

public void setPicture(Picture picture) {
this.picture = picture;
}

public Picture getPicture() {
return picture;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics