读取sql server中二进制图片

public void showPhoto(HttpServletResponse response,String fphotoId){
Connection conn = null;
try{
try {
InitialContext ic = new InitialContext();
Statement stmt = null;
conn = DBConnection.getConnection();
BufferedInputStream inputimage = null;
stmt = conn.createStatement();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
ResultSet rs = stmt.executeQuery("SELECT * FROM t_photo WHERE fid='"+ fphotoId + "'");
if (rs.next()) {
Blob blob = (Blob) rs.getBlob("fimage");
long size = blob.length();
byte[] bs = blob.getBytes(1, (int) size);
response.setContentType("image/jpeg;charset=GB2312");
response.setHeader("Content-Transfer-Encoding", "base64");
String filepath = "";
String dir = this.getServlet().getServletContext().getRealPath("UPLOAD");
File dirFile = new File(dir);
if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
dirFile.mkdir();
}
String filename = getRndFileName("jpg");
filepath = fphotoId+ filename.substring(filename.lastIndexOf("."),filename.length());
OutputStream out1 = new FileOutputStream(dir + "\\" + filepath);
out1.write(bs, 0, bs.length);
out1.close();
rs.close();
} else {
rs.close();
}
} catch (Exception e) {
conn.rollback();
e.printStackTrace();
} finally {
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}

/**
* @param path
* @return 返回随机获得的文件名 获取随机文件名.如 aa.gif bb.jpg 等.
*/
public String getRndFileName(String path) {
String rndFileName = String.valueOf(System.currentTimeMillis())
+ String.valueOf(new Random().nextInt(100));
String extFileName = getExName(path);
if (!extFileName.equals("")) {
rndFileName = rndFileName + "." + extFileName;
}
return rndFileName;
}

// 获取文件扩展名.如: gif jpg (不包括".")等.
public String getExName(String path) {
int strint = path.lastIndexOf(".");
return path.substring(strint + 1);// 这样是不包括点
// return path.substring(strint);//这样是有包括点
}

将读取的图片上传到服务器中。。。。
if(productList.size() != 0){
for (Product product : productList) {
if(product.getPhoto() != null){
String fphotoid = product.getPhoto().getFid();
String filepath = "";
String dir = this.getServlet().getServletContext().getRealPath("UPLOAD");
filepath = dir + "\\"+fphotoid+".jpg";
File file = new File(filepath);
if(!file.exists()){
showPhoto(response,fphotoid);
}
}
}
request.setAttribute("productList",productList);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值