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);
}
分享到:
相关推荐
使用delphi读写sqlserver中image字段,可以读取dmp,jpg,gif等格式的图片,至于二进制文件没有尝试过。
读取SQL数据库二进制图片并生成文件到本地
Matlab中二进制数据的读取2.doc
使用简单的SQL语法,查询二进制字段存储内容
如何把图片以二进制方式存入SQL_Server数据库_并能读取出来
opc服务器kepserver读取SQL Server数据库具体方法
java读取sqlserver image字段.docx
两个字段 id和Image类型的content字段(用来存放图片用二进制读取的内容)还有一个存储过程(我喜欢用存储过程)数据库里面有内容(那个女的不是我.)打开项目时请将数据库附加到SQL server 2005. 项目的功能大家...
本文非常详细的介绍了在matlab中如何导入和导出其他格式的数据。所讲的知识点要比书上所说更全面,更深刻,更容易理解原理。同时,还有大量的进行相互比较的例子
XE10的,里面代码也适用7,主要就是流操作,代码注释里讲了几个注意的细节,附加测试数据库;
.
.
.
。。。
主要介绍了C#实现把图片转换成二进制以及把二进制转换成图片的方法,结合具体实例形式分析了基于C#的图片与二进制相互转换以及图片保存到数据库的相关操作技巧,需要的朋友可以参考下
主要介绍了Java 中二进制转换成十六进制的两种实现方法的相关资料,需要的朋友可以参考下
asp.net从数据库中读二进制生成word文件并转换为MHT文件
C#中读取sql server的数据,并在datagridview中显示