如何在Postgres数据库当中存储和读取图片,用什么类型的字段,代码如何写啊 图片可以用bytea类型(也可大对象)代码看你要用什么语言了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 bytea类型对应java中的什么方法啊例如: get???() get不来的,要读取图片字节流存入数据库中,显示图片也要自己写方法的。如果用大对象可以调用大对象处理函数(参见http://fanqiang.chinaunix.net/a2/b3/20010421/231518.html)不过一般大对象是存放比较大的文件的,存放图片好像没必要用这个。 下面是java读取图片存入数据库的(bytea类型):如果图片不在WEB服务器上,需要上传服务器,然后下面是JAVA操作代码(数据库中是bytea类型):String photopath="详细的文件路名(包含图片名)" File file = new File(photopath);FileInputStream fis = new FileInputStream(file);int fileLength = (int)file.length(); photoStr = "insert into tab_photo(id,photopath) values('" + id + "',?)";statement = conn.prepareStatement(photoStr); //conn 是建立的数据库连接 statement.setBinaryStream(1, fis, fileLength);statement.executeUpdate();statement.close();fis.close(); 我写过一个用于显示图片的jsp,你再你的jsp中引用它就行了,这样引用:<img name="picshow" width="100" height="133" src="showImage.jsp?id=<%=id%>" title="图像预览"/>,其中的参数id是显示图片的jsp即showImage.jsp中用于唯一标志某个图片的一个参数,在showImage.jsp中要用这个id参数将所要显示的图片读出来,showImage.jsp中开始的数据库连接部分你应该改一下,我是tomcat连接池的,估计与你的不一样,数据库查询语句自己改。showImage.jsp:<%@page contentType="text/html;charset=GBK" language="java" %><%@ page buffer="200kb" %><%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>//这两行struts标签,不用可以删掉<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>//<%@page import="java.util.*" %><%@page import="java.sql.*" %><%@page import="javax.naming.*" %><%@page import="com.wellsoon.police.db.*" %><%@page import="java.io.*"%> <% try { String id=request.getParameter("id");//此处联数据库 Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { Context initCtx = new InitialContext(); Context ctx = (Context) initCtx.lookup("java:comp/env"); javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/police"); conn = ds.getConnection(); //stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); } catch (Exception e) { System.out.println(e.toString()); }String cmd="select case when photo is null then (select photo from tab_prephoto where id='aaa') else photo end from tab_photo WHERE id ='"+ id + "'";System.out.println("cmd.................=" + cmd);stmt=conn.prepareStatement(cmd); rs = stmt.executeQuery();byte[] Buffer=new byte[1024*10];InputStream InData=null;OutputStream outData=null;int iSize;if(rs.next()){outData=response.getOutputStream();InData=rs.getBinaryStream("photo");response.setContentType("image/gif,jpeg");while(true){iSize=InData.read(Buffer);if(iSize==-1){break;}outData.write(Buffer,0,iSize);}outData.flush();response.flushBuffer();}else {};rs.close();InData.close();outData.close();try {stmt.close();conn.close(); } catch (Exception e1) {out.println(e1.toString()); } finally {try { if(stmt != null) stmt.close(); if(!conn.isClosed()) conn.close();}catch (Exception e2) { out.println(e2.toString());} }} catch(Exception e) { System.out.println("error while close connection = "+e.toString());} %> 对了,有没有将图片读出来显示在jsp里的例子啊 我用的是jsp smartupload组件上传图片的,用的它的upBean.setMaxFileSize()来限制上传图片大小的,你要用其它方法自己搜索看看吧 可以用上传组件比如jsp smartupload,struts自带也有,自己到网上搜索,相关资料很多 进来看了一下,怎么好像说相声啊。我们公司都是用bytea来存图片和铃声的二进制数据。用的java,所以比较简单。 postgresql 能删除数据文件 mysql中如查询最近一定数量的记录 mysql下hash分区一个很郁闷的问题,大虾帮忙解决 HP-UX上执行mysql_init就core win7家庭版能装mysql吗? 局域网读mysql数据报错 你一定得进来看看,帮兄弟一把亚! EMSMySqlManager有linux版本的吗? mysql中通過phpmyadmin可以建存儲過程嗎?如何建? mysql 如图A表和B表怎么才能得到下面的结果 , 求高人! 如何使用MYSQL命令连接到远程服务器并执行某一文件中的SQL语句? 为什么我的Mysql-Front不能建立表格!
例如: get???()
如果用大对象可以调用大对象处理函数(参见http://fanqiang.chinaunix.net/a2/b3/20010421/231518.html)
不过一般大对象是存放比较大的文件的,存放图片好像没必要用这个。
如果图片不在WEB服务器上,需要上传服务器,然后下面是JAVA操作代码(数据库中是bytea类型):
String photopath="详细的文件路名(包含图片名)"
File file = new File(photopath);
FileInputStream fis = new FileInputStream(file);
int fileLength = (int)file.length();
photoStr = "insert into tab_photo(id,photopath) values('" + id + "',?)";
statement = conn.prepareStatement(photoStr); //conn 是建立的数据库连接
statement.setBinaryStream(1, fis, fileLength);
statement.executeUpdate();
statement.close();
fis.close();
<img name="picshow" width="100" height="133" src="showImage.jsp?id=<%=id%>" title="图像预览"/>,其中的参数id是显示图片的jsp即showImage.jsp中用于唯一标志某个图片的一个参数,在showImage.jsp中要用这个id参数将所要显示的图片读出来,showImage.jsp中开始的数据库连接部分你应该改一下,我是tomcat连接池的,估计与你的不一样,数据库查询语句自己改。
showImage.jsp:<%@page contentType="text/html;charset=GBK" language="java" %>
<%@ page buffer="200kb" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>//这两行struts标签,不用可以删掉
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>//
<%@page import="java.util.*" %>
<%@page import="java.sql.*" %>
<%@page import="javax.naming.*" %>
<%@page import="com.wellsoon.police.db.*" %>
<%@page import="java.io.*"%>
<%
try {
String id=request.getParameter("id");//此处联数据库
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/police");
conn = ds.getConnection();
//stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch (Exception e) {
System.out.println(e.toString());
}
String cmd="select case when photo is null then (select photo from tab_prephoto where id='aaa') else photo end from tab_photo WHERE id ='"+ id + "'";
System.out.println("cmd.................=" + cmd);
stmt=conn.prepareStatement(cmd);
rs = stmt.executeQuery();byte[] Buffer=new byte[1024*10];
InputStream InData=null;
OutputStream outData=null;
int iSize;
if(rs.next())
{
outData=response.getOutputStream();
InData=rs.getBinaryStream("photo");
response.setContentType("image/gif,jpeg");
while(true)
{
iSize=InData.read(Buffer);
if(iSize==-1)
{
break;
}
outData.write(Buffer,0,iSize);
}
outData.flush();
response.flushBuffer();
}else {};
rs.close();
InData.close();
outData.close();try {
stmt.close();
conn.close();
}
catch (Exception e1) {
out.println(e1.toString());
}
finally {
try {
if(stmt != null) stmt.close();
if(!conn.isClosed()) conn.close();
}
catch (Exception e2) {
out.println(e2.toString());
}
}
}
catch(Exception e)
{ System.out.println("error while close connection = "+e.toString());}
%>
我们公司都是用bytea来存图片和铃声的二进制数据。用的java,所以比较简单。