當前位置:首頁 » 編程語言 » mysqljavablob

mysqljavablob

發布時間: 2022-07-09 06:29:59

A. java把圖片變成二進制流存入mysql中,欄位類型為blob。但是在插入的時候總是報錯,不能識別二進制流

欄位排列不對應吧
INSERT INTO ()VALUES() 最好是寫完整、對應

~~~~~~~~~~~

B. 使用java語言操作,如何來實現MySQL中Blob欄位的存取

/**
* Title: BlobPros.java
* Project: test
* Description: 把圖片存入mysql中的blob欄位,並取出
* Call Mole: mtools資料庫中的tmp表
* File: C:downloadsluozsh.jpg
* Copyright: Copyright (c) 2003-2003
* Company: uniware
* Create Date: 2002.12.5
* @Author: ChenQH
* @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:Downloadsluozsh1.jpg");
blob.blobRead("c:/downloads/1.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}

C. java mysql資料庫 Blob亂碼 求救。

應該是
字元集
的問題。
最好都設成utf-8
包括資料庫,後台,還有前台頁面的編碼

D. Mysql的blob問題。

圖片是不大,可是圖片轉換為二進制編碼文件後就大多了

E. jsp/java讀取mysql的blob類型欄位中文亂碼問題 代碼如下,但是出來的是亂碼

知道了.
(2):手動插入中文數據,insert.....然後在mysql的窗口中查看是否正常顯示.
(3):根據測試結果確定是頁面問題還是資料庫設置問題
1:如果是頁面問題,相信你知道,怎麼解決.
2:如果是數據插入以後就變成了亂碼,那就是你的資料庫字元集有問題,找到my.ini
[client]
port=3306
[mysql]
加上:default-character-set=gbk
也可以通過資料庫設置向導來設置(windows下才有)
3:如果還是亂碼的話那就把頁面和資料庫的字元設置成一樣的就好了
希望對你有幫助
一般還是建議使用jbk
另外,虛機團上產品團購,超級便宜

F. java中使用kindEditor,mysql中用blob類型的欄位來存編輯器內容,出現中文亂碼

response.setCharacterEncoding("utf-8") ; //設置字元集編碼
String content= new String(req.getParameter("content").getBytes("ISO-8859-1"),"GBK") ;

G. 使用JAVA向MYSQL的BLOB類型,插入一個大文件,如何解決OutOfMemoryError問題

解決outofMemoryError是因為,系統給JVM分配的內存空間太小,你可以設置JVM的內存空間:
在你運行的時候,加上一個運行參數:
java -Xms512m -Xmx512m
這個是把你的jvm的內存的最小值和最大值分別設置為512m,

你試試。。。

H. java 以二進制流的方式讀取mysql 中的blob文件,並寫入本地文件夾

//配置資料庫連接驅動

String sql = xxxxxxxx;//要查詢的sql
PreparedStatement ps = conn.prepareStatement(sql);
String path = xxxxxxx;
ResultSet rs = ps.executeQuery();
while (rs.next()) {
InputStream is = rs.getBlob(x).getBinaryStream();//x為要取的BLOB位置

FileOutputStream os = new FileOutputStream(path + "//"
+ "存放的文件名"+「.zip」);
byte[] buff = new byte[1024];
while ((is.read(buff)) != -1) {
os.write(buff);
}
os.close();
is.close();
}
ps.close();
conn.close();

I. Mysql中的Blob

.hbm.xml文件中
<property name="image" type="java.sql.Blob">
<column name="image" />
</property>

對應的pojo類的類型也應為java.sql.Blob

mysql中對應的數據類型為 blob

"可是我手動改成java.lang.Blob欄位時就用不了了"
應該是java.sql.Blob 不是java.lang

ps:實際項目中,對於圖片處理多數採用資料庫中存圖片的路徑,一般不直接存圖片,因為載入圖片時大量數據從資料庫中讀取導致效率非常低

J. mysql blob在java中轉化成string類型,列印出來[B@a99013是什麼類型的數據,怎樣在頁面顯示

從mysql中去取出的是Blob類型,要轉String通過new String(blob.getBytes((long)1, (int)blob.length()));來轉,就是想要的數據

熱點內容
墨泥加密鎖 發布:2025-03-12 02:15:40 瀏覽:340
我的世界網易開局就32k的伺服器 發布:2025-03-12 02:07:42 瀏覽:695
怎麼用電腦搭建免費伺服器 發布:2025-03-12 02:01:43 瀏覽:612
mysql存儲過程和函數 發布:2025-03-12 01:56:36 瀏覽:188
srt流媒體伺服器搭建 發布:2025-03-12 01:55:37 瀏覽:547
如何查找ftp電影下載資源 發布:2025-03-12 01:37:37 瀏覽:387
一加保存的密碼在哪裡 發布:2025-03-12 01:13:06 瀏覽:90
微信第三方平台源碼 發布:2025-03-12 01:12:21 瀏覽:782
伺服器關閉怎麼補償 發布:2025-03-12 01:01:26 瀏覽:335
c語言復數的四則運算 發布:2025-03-12 01:01:22 瀏覽:804