java代碼如何實現(xiàn)存取數(shù)據(jù)庫的blob字段
一.業(yè)務
在業(yè)務中我們被要求將文件或圖片等轉(zhuǎn)成 byte[]
或 InputStream
存到數(shù)據(jù)庫的Blob
類型的字段中.
二.Blob類型介紹
在 MySQL 中,Blob 數(shù)據(jù)類型用于存儲二進制數(shù)據(jù)。MySQL 提供了四種不同的 Blob 類型:
TINYBLOB
: 最大存儲長度為 255 個字節(jié)。BLOB
: 最大存儲長度為 65,535 個字節(jié)。MEDIUMBLOB
: 最大存儲長度為 16,777,215 個字節(jié)。LONGBLOB
: 最大存儲長度為 4,294,967,295 個字節(jié)。
三. Blob 對應的 Java 類型
在 Java 中讀取 MySQL Blob 類型時,通常使用 java.sql.Blob
類型。java.sql.Blob
是一個接口,它提供了一些方法來操作 Blob 數(shù)據(jù)。
根據(jù) MySQL Blob 類型的不同,我們可以使用不同的 Java 類型來存儲 Blob 數(shù)據(jù)。
- TINYBLOB 對應
byte[]
或InputStream
。 - BLOB 對應
byte[]
或InputStream
。 - MEDIUMBLOB 對應
byte[]
或InputStream
。 - LONGBLOB 對應
byte[]
或InputStream
。
我們可以根據(jù)需要選擇合適的 Java 類型。推薦用InputStream
,這樣代碼不用轉(zhuǎn)換來轉(zhuǎn)換去,比較簡單
四.上存取java代碼
1.建表
2.建實體類
@Data public class TTT { private String id; private String name; private String createTime; private byte[] miaoshuByte; private InputStream miaoshuInputstream; }
3.用個自己寫的工具類
public class FileUtil { /** * file轉(zhuǎn)byte */ public static byte[] file2byte(File file) throws IOException { FileInputStream fis = null; ByteArrayOutputStream bos = null; try { fis = new FileInputStream(file); bos = new ByteArrayOutputStream(); IOUtils.copy(fis, bos); byte[] bytes = bos.toByteArray(); return bytes; }finally { if (fis != null) { fis.close(); } if (bos != null) { bos.close(); } } } /** * byte 轉(zhuǎn)file */ public static File byte2File(byte[] buf,String fileName) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(fileName); fos.write(buf); File file = new File(fileName); return file; } finally { if (fos != null) { fos.close(); } } } }
4.訪問接口
@RestController @RequestMapping("order/") @Slf4j public class SendHttpWController { @Autowired private UtimeeMapper utimeeMapper; @GetMapping("/aa") public String queryById( Integer id) throws IOException { TTT ttt = new TTT(); ttt.setId("30"); ttt.setName("張三"); File file = new File("F:\\Desktop\\aa.docx"); byte[] bytes = FileUtil.file2byte(file); ttt.setMiaoshuByte(bytes); FileInputStream fileInputStream = new FileInputStream(file); ttt.setMiaoshuInputstream(fileInputStream); utimeeMapper.insert01(ttt); return "嘿嘿額黑8082"; } @GetMapping("/bb") public String bb( Integer id) throws IOException { TTT ttt = utimeeMapper.select01("30"); byte[] bytes = ttt.getMiaoshuByte(); FileUtil.byte2File(bytes,"F:\\Desktop\\cc.docx"); InputStream inputStream = ttt.getMiaoshuInputstream(); FileOutputStream outputStream = new FileOutputStream("F:\\Desktop\\dd.docx"); IOUtils.copy(inputStream, outputStream);//記得添加關流代碼(本代碼省略了) return "嘿嘿額黑8082"; }
5.輸出成果
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Mybatis Order by動態(tài)參數(shù)防注入方式
這篇文章主要介紹了Mybatis Order by動態(tài)參數(shù)防注入方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04Elasticsearch?Analyzer?內(nèi)置分詞器使用示例詳解
這篇文章主要為大家介紹了Elasticsearch?Analyzer?內(nèi)置分詞器使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11在Spring Boot中實現(xiàn)文件上傳與管理的操作
在 Spring Boot 中實現(xiàn)文件上傳與管理非常簡單,通過配置文件上傳、創(chuàng)建文件上傳、下載、列表和刪除接口,我們可以輕松地處理文件操作,結(jié)合前端頁面,可以提供一個完整的文件管理系統(tǒng),這篇文章主要介紹了在Spring Boot中實現(xiàn)文件上傳與管理,需要的朋友可以參考下2024-07-07淺談Spring Boot、MyBatis、MyBatis-Plus 依賴版本對應關系
本文主要介紹了SpringBoot、MyBatis和MyBatis-Plus的依賴版本對應關系,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-11-11