显示大字段的程序
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="Dbcon.ConnectionPool"%>
<jsp:useBean id="mydb" scope="page" class="Dbcon.ConnectionPool" />
<%
String jh=new String(request.getParameter("jh").getBytes("ISO8859_1"));
Connection conn=mydb.getCononnection();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
Statement stmt=conn.createStatement();
String sql="select SJBG from DCB01 where jh='"+jh+"' and sjrq=to_date('"+request.getParameter("sjrq")+"','yyyy-mm-dd')";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if(rs.getBlob("SJBG")==null){out.print("<h1><font color='red'>没有检索到报告!!</font></h1>");break;}
java.sql.Blob blob = rs.getBlob("SJBG");
BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream());
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
response.reset();
response.setContentType("application/msword");
int c;
while ((c=in.read())!=-1) {
buffout.write(c);
}
in.close();
buffout.close();
}
} catch (Exception ex) {throw ex;}
finally{
mydb.freeConnection(conn);
}
%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="Dbcon.ConnectionPool"%>
<jsp:useBean id="mydb" scope="page" class="Dbcon.ConnectionPool" />
<%
String jh=new String(request.getParameter("jh").getBytes("ISO8859_1"));
Connection conn=mydb.getCononnection();
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
Statement stmt=conn.createStatement();
String sql="select SJBG from DCB01 where jh='"+jh+"' and sjrq=to_date('"+request.getParameter("sjrq")+"','yyyy-mm-dd')";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
if(rs.getBlob("SJBG")==null){out.print("<h1><font color='red'>没有检索到报告!!</font></h1>");break;}
java.sql.Blob blob = rs.getBlob("SJBG");
BufferedOutputStream buffout = new BufferedOutputStream(response.getOutputStream());
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
response.reset();
response.setContentType("application/msword");
int c;
while ((c=in.read())!=-1) {
buffout.write(c);
}
in.close();
buffout.close();
}
} catch (Exception ex) {throw ex;}
finally{
mydb.freeConnection(conn);
}
%>
解决方案 »
- 我从服务器那边传递过来的json数据解析不了
- 求解决线程同步问题
- 求尚学堂视频
- remove操作后的xml进行socket传输问题
- 求助啊:我在WEB.xml中配置的servlet需要传配置文件的相对路径,老是出错啊?
- 熟悉Proxool连接池的朋友进来下!有个低级问题!
- 讨论EJB群!10057492 欢迎加入!
- log4j的mail配置疑问
- BMP事务的奇怪问题,大家过来看看。
- 结交JAVA爱好者!!!!!!!!!!
- 学习jsp,oracle,环境apache+resin。请介绍一些相关的学习资料,感激!!
- 紧急求解:我有一512M DDR服务器内存,浪潮服务器的,现在想把它使用在普通的PC上?为何检测不出呢?是不是与我的主板有关系呢?是不是在
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.jspsmart.upload.*"%>
<%@ page import="sjdb.Sjdb"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="sjdb" scope="page" class="sjdb.Sjdb" />
<html>
<body bgcolor="#FFFFFE" leftMargin=0 topMargin=0> <%
String execept=null;
java.util.Hashtable ht=new Hashtable();
try{ mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
byte binBytes[] =null;
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String[] values = mySmartUpload.getRequest().getParameterValues(key);
ht.put(key,values[0]);
} for (int i=0;i<mySmartUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
binBytes= new byte[myFile.getSize()];
for (int j=0;j <myFile.getSize();j++)
{
binBytes[j] = myFile.getBinaryData(j);
}
ht.put("TP",binBytes);
ht.put("TXLX",myFile.getFieldName());
if(myFile.getFileExt().equals("JPEG")){
ht.put("TPGS","1");
}else if(myFile.getFileExt().equals("GIF")){
ht.put("TPGS","2");
}else{
ht.put("TPGS","3");
}
sjdb.inserDb(ht);
execept=sjdb.getExcept();
if(execept!=null){
%>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.history.back(-1);
alert('<%=execept.trim()%>');
//-->
</SCRIPT>
<%}
}
}
String sjlx=(String)ht.get("sjlx");
if(execept==null){
response.sendRedirect("../../ShiJing/DCA20Add.jsp?sjlx="+sjlx);
}
}catch(Exception e){
out.println(e);
}%></body>
</html>
怎么利用struts的结构来进行操作呀。
我只写好了一个存储!利用struts 和hibernate我在这里简单的说一下!
页面利用自己已经定义好的form。标签:<html:file property="pic"/>
数据库中定义: 字段类型为blob 当有数据是现实二进制的数据
xxx.hbm.xml 中这样定义: <property name="u009" column="U009" />
form中定义: 类型 import org.apache.struts.upload.FormFile;
hbm 中定义: 类型 为 import java.sql.Blob; (好多资料上用的是oracle.sql.BLOB 我还不知道优化了什么,只知道oracle是java的子类)。
action 中也是最重要的,方法大致如下:
byte[] buffer = new byte[1];
buffer[0] = 1;
user.setU009(Hibernate.createBlob(buffer));
SysUserUtil.getInstance().create(user);
Session session = HibernateUtil.openSession(); //保存数字的签名
session.refresh(user, LockMode.UPGRADE);
BLOB blob = (BLOB) user.getU009();
OutputStream out = blob.getBinaryOutputStream();
out.write(userForm.getU009().getFileData());
SysUserUtil.getInstance().save(user);
由于我是在用户表中添加的图片所以我的命名是用的user 大致的思路就是,现给这个字段给一个空对象,然后在更新这个字段,其实我也不知道为什么要先给一个空值,然后再更新,为什么不能一次到位???大致就这些,这两天休息还没有写显示的功能,有了马上给大家贴出来!
希望关注的朋友多多帮忙,我觉得我写的问题很多!!其实感觉还是用struts比较简单。
byte[] buffer = new byte[1024];
buffer[0] = 1; //QQQ
user.setU009(Hibernate.createBlob(buffer)); //QQQ
SysUserUtil.getInstance().create(user);
Session session = HibernateUtil.openSession(); //保存数字的签名
session.refresh(user, LockMode.UPGRADE);
BLOB blob = (BLOB) user.getU009();
OutputStream out = blob.getBinaryOutputStream();
buffer = new byte[100000];
System.out.println("blob's form size: " + userForm.getU009().getFileSize());
InputStream in = userForm.getU009().getInputStream();
int begin = 0;
int fileSize = userForm.getU009().getFileSize();
if(in != null){
while((begin = in.read(buffer)) != -1){
out.write(buffer,0,begin);
}
//out.write(userForm.getU009().getFileData());
SysUserUtil.getInstance().save(user);
改进了一下!!
===================================
页面上的也写好了代码如下!!
<html>
<head>
<%@ page import="java.sql.*,java.util.*,
com.hl.jls.security.model.helper.SysUserHelper,
com.hl.jls.security.model.hbm.*, java.sql.Blob,java.io.*,
com.hl.jls.utill.WebUtil"
%>
</head><body>
<%
response.reset();
response.setContentType("image/jpeg"); SysUserHelper sysuser = new SysUserHelper();
List user = (List) sysuser.getUsersPicBynameU007("xz","25901");
SysUser sysu = (SysUser) user.get(0);
Blob u009 = sysu.getU009();
byte[] blobbytes = null;
InputStream in = null;
ServletOutputStream sos=null; int blobsize = (int)u009.length();
blobbytes = new byte[blobsize];
in = u009.getBinaryStream();
int bytesRead= 0;
sos = response.getOutputStream(); while((bytesRead = in.read(blobbytes)) != -1){
sos.write(blobbytes, 0, bytesRead);
}
sos.flush();
in.close();
sos.close();
%>
</body>
</html>
我把方法集成到一个类里了WebUtil.class里:
/**
* 更具用户名和职务编码查询用户签名
* @param name
* @param u007
* @return
*/
public static byte[] getNamePic(String name, String u007) {
SysUserHelper sysuser = new SysUserHelper();
List user = (List) sysuser.getUsersPicBynameU007(name, u007);
SysUser sysu = (SysUser) user.get(0);
Blob u009 = sysu.getU009();
byte[] b = null;
InputStream in = null;
try {
int blobsize = (int)u009.length();
b = new byte[blobsize];
in = u009.getBinaryStream();
int len= 0;
while((len = in.read(b)) > 0){
System.out.println(len);
}
} catch (SQLException ex1) {
ex1.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
in.close();
} catch (IOException ex2) {
ex2.printStackTrace();
}
}
return b;
}
========================================
问题也来了, response.setContentType("image/jpeg"); 这句话和我以前页面上的 <%@ page contentType="text/html; charset=GBK" %> 有冲突 如果两句都有就会有乱吗,怎么办呀!!
还有就是小图片显示不出来,大概1kb左右的,不知道大家遇到这样的问题没有。
大图片最下面的几行也显示不出来,也不知道哪里错了。自己发了n次了,可也没人关心自己加油吧!!
================================
保存images进SQL Server数据库Stream imgdatastream = File1.PostedFile.InputStream;int imgdatalen = File1.PostedFile.ContentLength;string imgtype = File1.PostedFile.ContentType;string imgtitle = TextBox1.Text;byte[] imgdata = new byte[imgdatalen];int n = imgdatastream.Read(imgdata,0,imgdatalen);string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );paramTitle.Value = imgtitle;command.Parameters.Add( paramTitle);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );paramData.Value = imgdata;command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );paramType.Value = imgtype;command.Parameters.Add( paramType );
connection.Open();int numRowsAffected = command.ExecuteNonQuery();connection.Close();======================================
从数据库中输出图片private void Page_Load(object sender, System.EventArgs e){string imgid =Request.QueryString["imgid"];string connstr=((NameValueCollection)Context.GetConfig("appSettings"))["connstr"];string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "+ imgid;SqlConnection connection = new SqlConnection(connstr);SqlCommand command = new SqlCommand(sql, connection);connection.Open();SqlDataReader dr = command.ExecuteReader();if(dr.Read()){Response.ContentType = dr["imgtype"].ToString();Response.BinaryWrite( (byte[]) dr["imgdata"] );}connection.Close();}
在页面:
<html:file property=file/>
Form:在ActionForm中添加属性:private FormFile file属性Action:添加临时变量FormFile ,获得它的OutputStream,然后就可以向数据库里写了,如果是oracle数据库,就用Blog对象就行了,查查书很容易找到存取大对象的例子.
一定没有看我上面的发言吧!这中方法我已经实现了,现在问题是:
第一: 图片老是有部分显示不出来,原因还没有找到,我认我可能导致原因的地方第一,显示图片的时候 存放blob的数组过小,或者读入的时候就有问题
第二: 修改的时候我还需要把这个字段在设为 emptu_blob() 吗?
由于个人能力有限所以试了好多次都没有成功!
对了听说有个工具能看到看到oracle 下图片字段的图像,直到的可以给我说一下吗?先谢了
抱歉,我是没有仔细看上面的问题,我的看法也只是很肤浅的看法
Oracle提供了一个表示二进制数据的类:oracle.sql.BLOB
因为普通的SQL语句不能表示Blob数据,在执行插入数据到数据库的语句时可以使用:
insert into XXX_table values(empty_blob()) 插入一个空值到数据库,
empty_Blob返回的是一个空的Blob数据.往数据库中添加Blob数据时,首先通过ResultSet.getBlob()
方法获得Blob数据对象,然后通过:
OutputStream outstream = blob.getBinaryOutputStream()语句来获得这个Blob的输出流,有了这个
输出流就可以写入数据到数据库中,但是如果使用数据库持久层我就不太熟了.呵呵
找到了一个能提供oracle中blob字段预览图片的 工具 embarcadero Rapid sql 版本号7.2.1
挺好用的一个工具,在某些方面比 Navigator 好用本来打算这两天结贴的,可问题还是没有解决呀!
好像没有恨么好的方法,实在不行我就用jdbc写存储,唉!!
怪想不通的,为什么保存的图片会有一部分没有那?
InputStream 放进的数组大小也够呀!这是为什么???????????????说一个问题,jdbc创建blob时用的是empty_blob()
而用持久化创建的时候我是这样做的,给form中BLOB字段负值
byte[] buffer = new byte[10000];
user.setU009(Hibernate.createBlob(buffer)); //u009为blob类型的字段
不知道这样会不会影响后面的覆盖???谢谢大家的关注,问题解决了我一定发篇文章好好总结一下!
sqlserver中的image.
上传用common-upload.jar,写入jdbc方法: setBytes(index, byte[]);显示时,文件类型根据文件后缀从资源类中取出相应类型,1。传给前台byte[]
request.setAttribute("RESULT", ...);
2. 用response 的out直接write。
struts,hibernate可以分开用也可以合起来 ,不过大部分人都结合使用。
刚学如果没有人教的话,学起来很费劲的。
加油呀!