導航:首頁 > 編程語言 > javablob轉string

javablob轉string

發布時間:2025-04-08 13:01:24

java 關於blob類型問題

public void save(String vid,String title,String type,String user,String date,String context,String file) throws Exception
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "insert into news(v_id,title,type,person,inputtime,context,attach_docid) values('"+vid+"','"+title+"','"+type+"','"+user+"','"+date+"',EMPTY_BLOB(),'"+file+"')";
try {
DBJdbc dbjdbc = new DBJdbc();
conn = dbjdbc.getDBConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
String sql1 = "select context from news where v_id ='"+vid+"' for update"; // 使用"FOR UPDATE"得到表的寫鎖
rs = stmt.executeQuery(sql1);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob(1); // 得到BLOB對象
OutputStream outout = blob.getBinaryOutputStream(); // 建立輸出流
InputStream in = new ByteArrayInputStream(strss.HTMLEncode(context).getBytes()); //字元串轉換為數據流
int size = blob.getBufferSize();
byte[] buffer = new byte[size]; // 建立緩沖區
int len;
while ((len = in.read(buffer)) != -1)
outout.write(buffer, 0, len);
in.close();
outout.close();
}
conn.commit();
stmt.close();
conn.close();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
希望能幫助到你

② java String類型轉換為Blob類型怎麼轉

這個是mysql下存取blob欄位的一個很簡單的類,跟據自己的需要改改就行了

/**
* Title: BlobPros.java
* Project: test
* Description: 把圖片存入mysql中的blob欄位,並取出
* Call Mole: mtools資料庫中的tmp表
* File: C:\downloads\luozsh.jpg
* Copyright: Copyright (c) 2003-2003
* Company: uniware
* Create Date: 2002.12.5
* @Author: FeiFan
* @version 1.0 版本*
*
* Revision history
* Name Date Description
* ---- ---- -----------
* Chenqh 2003.12.5 對圖片進行存取
*
* note: 要把資料庫中的Blob欄位設為longblob
*
*/

//package com.uniware;

import java.io.*;
import java.util.*;
import java.sql.*;

public class BlobPros
{
private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public BlobPros()
{
}

/**
* 向資料庫中插入一個新的BLOB對象(圖片)
*
* @param infile - 要輸入的數據文件
* @throws java.lang.Exception
*
*/
public void blobInsert(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);

file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
pstmt.setString(1,file.getName()); //把傳過來的第一個參數設為文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //這種方法原理上會丟數據,因為file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二個參數為文件的內容
pstmt.executeUpdate();
}
catch(Exception ex)
{
System.out.println("[blobInsert error : ]" + ex.toString());
}
finally
{
//關閉所打開的對像//
pstmt.close();
fis.close();
conn.close();
}
}

/**
* 從資料庫中讀出BLOB對象
*
* @param outfile - 輸出的數據文件
* @param picID - 要取的圖片在資料庫中的ID
* @throws java.lang.Exception
*
*/

public void blobRead(String outfile,int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];

try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select pic from tmp where id=?");
pstmt.setInt(1,picID); //傳入要取的圖片的ID
rs = pstmt.executeQuery();
rs.next();

file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,則創建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //從資料庫中一段一段的讀出數據
//System.out.println(size);
if(size != -1) //-1表示讀到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}

}
catch(Exception e)
{
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
finally
{
//關閉用到的資源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}

public static void main(String[] args)
{
try
{

BlobPros blob = new BlobPros();
//blob.blobInsert("C:\\Downloads\\luozsh1.jpg");
blob.blobRead("c:/downloads/luozishang.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}

③ JAVA怎麼把圖片從資料庫中調用出來

1 一半圖片都是把路徑存放在資料庫的 到時候取出路徑就可以了
2 在資料庫有blob格式可以存放圖片 以二進制流的方式取出來

<% String zjbm = CheckParam(request.getParameter("zjbm"),""); String zpSql = "select zp from tjjryxxx where sfzh = '"+zjbm+"'"; out.clear(); response.setContentType("image/jpeg"); response.setHeader("Content-Transfer-Encoding","base64"); Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; Blob blob =null; byte[] data = null; try{ connection =getConn(); ps = connection.prepareStatement(zpSql); rs = ps.executeQuery(); while(rs.next()){ blob = (Blob)rs.getBlob("zp"); long nlen = blob.length(); int nsize = (int) nlen; data = blob.getBytes(1,nsize); OutputStream out1 = response.getOutputStream(); BufferedOutputStream bos =null; bos = new BufferedOutputStream(out1); bos.write(data,0,data.length); bos.close(); rs.close(); } }catch(Exception e){ e.printStackTrace(); } %>

④ java 中 blob轉字元串

在處理Java中的Blob轉字元串時,可以使用BufferedInputStream從Blob對象中讀取二進制流。具體代碼如下:

BufferedInputStream bi = new BufferedInputStream(blob.getBinaryStream());

這里定義了一個BufferedInputStream對象bi,它從Blob對象獲取二進制流。接下來,定義了一個byte數組data,用於存儲每次讀取的二進制數據。

data = new byte[READ_BUFFER_SIZE];

這里設置了一個固定大小的byte數組,用於存儲每次讀取的數據。READ_BUFFER_SIZE是一個預設的緩沖區大小,可以根據實際情況調整。

在循環中,通過bi.read(data)方法讀取數據到data數組中。每次讀取的長度由len變數表示,如果len值不等於-1,則表示讀取成功。

for (int len = 0; (len = bi.read(data)) != -1;)

在循環內部,將讀取的數據轉換為字元串並追加到outfile變數中。使用GBK編碼將byte數組轉換為字元串。

outfile += new String(data, "GBK");

這里需要注意的是,在每次循環中需要重新初始化data數組。這是因為read方法只是覆蓋讀取的數據,並不會清空整個數組,因此需要在下一次讀取前重新分配一個新數組。

data = new byte[READ_BUFFER_SIZE];

通過這種方式,可以確保每次讀取的數據都被正確處理,並且不會影響到下一次的讀取操作。

閱讀全文

與javablob轉string相關的資料

熱點內容
程序員放棄後會怎樣 瀏覽:158
河北模具編程 瀏覽:177
adb查找命令 瀏覽:308
安卓手機視頻文件夾怎麼打開 瀏覽:302
平板加密手機後怎麼關閉 瀏覽:555
流媒體伺服器應該注意什麼 瀏覽:526
d8命令編譯 瀏覽:942
壓縮包解壓需要多少空間 瀏覽:138
如何查找app屬性 瀏覽:380
android人臉識別技術 瀏覽:304
pc104編程 瀏覽:328
二維碼反編譯破解推廣 瀏覽:673
修改伺服器的mac地址 瀏覽:519
好玩的編程軟體 瀏覽:891
編程語言創始人有錢嗎 瀏覽:796
短視頻app怎麼獲客 瀏覽:7
查看雲伺服器的應用 瀏覽:426
javadump工具 瀏覽:557
程序員16g 瀏覽:420
程序員沒有辦法成為top怎麼辦 瀏覽:195