將BufferedInputStream 轉換為映像
從資料庫轉換Blob 時遇到問題,您認為該Blob 是JPEG 格式的影像,進入BufferedImage進行進一步處理。轉換失敗,您的 Image 變數仍為空。
轉換失敗的可能原因
檢查您的程式碼後,有幾個潛在問題可能會導致轉換失敗:
- 資料擷取:程式碼使用以下方式擷取blob 資料blob.getBytes(1, blobLength)。但是,blobLength 是以 long(64 位元整數)形式取得的,而 getBytes 則需要 int(32 位元整數)。這種不匹配可能會導致資料截斷和不正確的影像檢索。
- 映像驗證:確保 uploadedInputStream 流包含有效的映像將是一個很好的起點。您可以使用 ImageIO.write 將映像暫時寫入文件,然後讀回以驗證其完整性。
- Blob 內容處理: 根據 H2 資料庫文檔,Blob 內容不會儲存在記憶體中。因此,您應該使用 blob.getBinaryStream 來檢索 blob 的流表示形式,而不是使用 blob.getBytes。
解決方案
解決問題,嘗試修改您的程式碼如下:
public Response post(@PathParam("id") String id) throws IOException { Connection con = connection(); Blob blob = getPhoto(con); BufferedImage image = null; InputStream blobStream = null; int blobLength = 0; try { blobLength = (int) blob.length(); blobStream = blob.getBinaryStream(1, blobLength); image = ImageIO.read(blobStream); } catch (SQLException e2) { e2.printStackTrace(); } return Response.ok(image).build(); }
此外,您應該驗證有效性通過將uploadedInputStream 寫入檔案並將其讀回以確保它包含映像。
以上是從資料庫轉換 Blob 時,為什麼我的「BufferedImage」為空?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中間佈洛布(16,777,215個比例),andlongblob(4,294,967,967,295 bytes).tousebl觀察:1)考慮pperformance impactsandSandStorLageBlobSextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自動化在MySQL中創建用戶的最佳工具和技術包括:1.MySQLWorkbench,適用於小型到中型環境,易於使用但資源消耗大;2.Ansible,適用於多服務器環境,簡單但學習曲線陡峭;3.自定義Python腳本,靈活但需確保腳本安全性;4.Puppet和Chef,適用於大規模環境,複雜但可擴展。選擇時需考慮規模、學習曲線和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)轉換theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchUsiseLike.2)forCompresseBlysBlobs,useuncompresseblobs,useuncompressbeforeconversion.3)expperformance impperformance imptactSandDataEcoding.4)

mysqloffersvariousStringDatatYpes:1)charforfixed Lengtth Strings,IdealforConsistLengthDatalikeCountryCodes; 2)varcharforvariable長度長,合適的forfieldslikenames; 3)texttypefesforepesforlargertext,forforlargertext,goodforforblogblogpostsbutcan impactcuctcuctcuctpercrance; 4)biland;

tomasterMysqlblobs,關注台詞:1)ChooseTheApprProbType(tinyBlob,blob,blob,Mediumblob,longblob)基於dongatasize.2)InsertDatausingload_fileforefice.3)

blobdatatypesinmysqlareusedforvorvoringlargebinarydatalikeimagesoraudio.1)useblobtypes(tinyblobtolonglongblob)基於dondatasizeneeds。 2)庫孔素pet petooptimize績效。 3)考慮Xternal Storage Forel Blob romana databasesizerIndimprovebackupe

toadDuserStomySqlfromtheCommandline,loginasroot,thenusecreateuser'username'@'host'host'Indessifiedby'password'; tocreateanewuser.grantpermissionswithgrantprantallprivilegesondatabase

mySqlofferSeightStringDatateTypes:char,varchar,二進制,二進制,varbinary,blob,文本,枚舉,枚舉和set.1)長度,理想的forconsistentDatatalIkeCountryCodes.2)varcharisvariable長度,長度,效率foriforitifforiticforiticforiticforiticforiticforitic forvaryingdatalikename.3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器