「java读取blod」java读取blob数据

博主:adminadmin 2022-12-25 23:30:09 91

今天给各位分享java读取blod的知识,其中也会对java读取blob数据进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java oracle数据blob的操作

// 下面为一个完整的例子,如果用framework,需要做一定修改

写入    

public boolean saveWordFile(String filePath) {

        File file = new 

File(filePath);

        Connection conn = getConnection();

        try 

{

            java.sql.Statement st = conn.createStatement();

            

conn.setAutoCommit(false);

            st.execute("insert into table_name 

values(1,empty_blob())");

            ResultSet rs = 

st

                    .executeQuery("select id,word from table_name where 

id=1 for update");

            if (rs.next()) {

                BLOB blob 

= (BLOB) rs.getBlob("word");

                OutputStream outStream = 

blob.getBinaryOutputStream();

                InputStream fin = new 

FileInputStream(file);

                byte[] b = new 

byte[blob.getBufferSize()];

                int len = 0;

                

while ((len = fin.read(b)) != -1) {

                    outStream.write(b, 0, 

len);

                }

                fin.close();

                

outStream.flush();

                outStream.close();

                

conn.commit();

                conn.close();

            }

        } 

catch (SQLException e) {

            // TODO Auto-generated catch 

block

            e.printStackTrace();

        } catch 

(FileNotFoundException e) {

            // TODO Auto-generated catch 

block

            e.printStackTrace();

        } catch (IOException e) 

{

            // TODO Auto-generated catch block

            

e.printStackTrace();

        }

        return true;

    }

读取

   public void getWordFile(String id) {

        Connection conn = 

getConnection();

        java.sql.Statement st;

        try 

{

            st = conn.createStatement();

            ResultSet rs = 

st.executeQuery("select id,word from table_name where 

id='"+id+"'");

            if (rs.next()) {

                BLOB blob = 

(BLOB) rs.getBlob("word");

                File file = new 

File("c://filename.doc");

                FileOutputStream output = new 

FileOutputStream(file);

                InputStream input = 

blob.getBinaryStream();

                byte[] buffer = new 

byte[1024];

                int i = 0;

                while ((i = 

input.read(buffer)) != -1) {

                    output.write(buffer, 0, 

i);

                }

            }

        } catch (Exception e) 

{

            // TODO Auto-generated catch block

            

e.printStackTrace();

        }

   }

修改

public void updateblob(String id){

  Connection 

conn=getConnection();

  Statement stem=null;

  ResultSet rs=null;

  

try{ 

         conn.setAutoCommit(false); 

         

stem=conn.createStatement();

         rs = stem.executeQuery("select word 

from table_name where id='"+id+"' for update"); 

         

         if 

(rs.next()) { 

             BLOB blob = (BLOB) rs.getBlob("word"); 

             OutputStream outStream = blob.getBinaryOutputStream(); 

             InputStream fin = new FileInputStream("c://2.doc"); 

             byte[] b = new byte[blob.getBufferSize()]; 

             int 

len = 0; 

             while ((len = fin.read(b)) != -1) { 

                 outStream.write(b, 0, len); 

             } 

             fin.close(); 

             outStream.flush(); 

             outStream.close(); 

             conn.commit(); 

             conn.close(); 

         } 

} catch (Exception ex){ 

 try {

  conn.rollback();

 } catch (SQLException e) {

  // TODO 

Auto-generated catch block

  e.printStackTrace();

 }

  

System.out.println(ex.getMessage());

}

 }

java读取blob并用base64码进行保存

转换没有问题。

但是读有问题。不要指望read可以一次性完成,虽然通常可以。

请检查read的返回值。

另外建议使用try(....){}格式!

base64非常简单,就是3*8=4*6的转换。

8表示一个卦8爻,6表示转换后的实际上一个卦6爻。

卦--byte

爻--bit

但是base64有几种不同格式,而且是谁用谁知道的方式。可以看看其他更详细的介绍。

java数据库blob字段的下载(读取)

这是我以前写的代码,放在action里。图片在pojo类中对应为byte[]类型,clxxb是一个pojo类,clxxb.getClpic()得到图片对应的字节数组byte[]。其实输出文件就是输出一个字节流。希望对你有帮助。

InputStream input=clxxb.getClpic().getBinaryStream();

byte[] buffer=new byte[input.available()];

ServletOutputStream out=response.getOutputStream();

int length=0;

while((length=input.read(buffer))!=-1){

out.write(buffer,0,length);

}

out.flush();

out.close();

java 怎么读取oracle的blob类型数据,我的是保存新闻内容! 只有这么多财富值了,帮个忙谢谢

BLOB blob = (BLOB) rs.getBlob(5);后,使用blob的getBytes()获取byte[]数据或者使用getBinaryStream() 获取流对象,这样你就可以输出到JSP页面了

java提取数据库中blob类型的图片,如何全部显示在jsp页面?

在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下:

/**

* 根据图片在数据库中的ID进行读取

* @param strID 图片字段ID

* @param w 需要缩到的宽度

* @param h 需要缩到高度

* @return

*/

public byte[] GetImgByteById(String strID, int w, int h){

//System.out.println("Get img data which id is " + nID);

if(myConnection == null)

this.getConnection();

byte[] data = null;

try {

Statement stmt = myConnection.createStatement();

ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);

StringBuffer myStringBuffer = new StringBuffer();

if (myResultSet.next()) {

java.sql.Blob blob = myResultSet.getBlob(this.strImgName);

InputStream inStream = blob.getBinaryStream();

try {

long nLen = blob.length();

int nSize = (int) nLen;

//System.out.println("img data size is :" + nSize);

data = new byte[nSize];

inStream.read(data);

inStream.close();

} catch (IOException e) {

System.out.println("获取图片数据失败,原因:" + e.getMessage());

}

data = ChangeImgSize(data, w, h);

}

System.out.println(myStringBuffer.toString());

myConnection.commit();

myConnection.close();

} catch (SQLException ex) {

System.out.println(ex.getMessage());

}

return data;

}

页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:

%@ page language="java" contentType="text/html;;charset=gbk" %

jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" /

%

response.setContentType("image/jpeg");

//图片在数据库中的 ID

String strID = request.getParameter("id");

//要缩略或放大图片的宽度

String strWidth = request.getParameter("w");

//要缩略或放大图片的高度

String strHeight = request.getParameter("h");

byte[] data = null;

if(strID != null){

int nWith = Integer.parseInt(strWidth);

int nHeight = Integer.parseInt(strHeight);

//获取图片的byte数据

data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);

ServletOutputStream op = response.getOutputStream();

op.write(data, 0, data.length);

op.close();

op = null;

response.flushBuffer();

//清除输出流,防止释放时被捕获异常

out.clear();

out = pageContext.pushBody();

}

%

java读取blod的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取blob数据、java读取blod的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。