將檔案儲存到資料庫(以二進位制流的形式)
Hibernate方法
HibernateUtils.java
package yang.fang.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * hibernate工具類 * * @author zhaoqx * */ public class HibernateUtils { private static Configuration conf; private static SessionFactory sf; static { conf = new Configuration().configure();// 讀取hibernate.cfg.xml sf = conf.buildSessionFactory(); } public static Session getSession() { return sf.openSession(); } public static void main(String[] args) { getSession(); } }
Person.java
package yang.fang.hibernate; public class Person { private Integer id; private String username; private String password; private String email; private String birthday; private String job; private String salary; private byte[] photo; // 頭像圖片 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public Person() { super(); } public Person(String username, String birthday, String salary) { super(); this.username = username; this.birthday = birthday; this.salary = salary; } @Override public String toString() { return "Person [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", birthday=" + birthday + ", job=" + job + ", salary=" + salary + "]"; } public byte[] getPhoto() { return photo; } public void setPhoto(byte[] photo) { this.photo = photo; } }
Person.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="yang.fang.hibernate"> <class name="Person" table="person"> <id name="id"> <generator class="native" /> </id> <property name="username" /> <property name="password" /> <property name="email" /> <property name="birthday" /> <property name="job" /> <property name="salary"/> <!-- 頭像,二進位制型別,最好指定長度 --> <property name="photo" type="binary" length="1024000"></property> </class> </hibernate-mapping>
TestHibernate.java
package yang.fang.hibernate;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;
public class TestHibernate {
// 增加測試記錄
@Test
public void test1() throws Exception {
Session session = HibernateUtils.getSession();
Transaction tr = session.beginTransaction();
InputStream in = new FileInputStream("F:/test.jpg");
byte[] photo = new byte[in.available()];
in.read(photo);
in.close();
Person person = new Person();
// person.setId(5);
person.setUsername("歐陽鋒");
person.setPassword("******");
person.setEmail("[email protected]");
person.setBirthday("1989-05-07");
person.setJob("上班");
person.setSalary("60000");
person.setPhoto(photo);
session.save(person);
tr.commit();
session.close();
}
}
取出圖片
public void testGet() throws Exception
<span style="font-family: Arial, Helvetica, sans-serif;">Session session = HibernateUtils.getSession();</span><pre name="code" class="html"> Transaction tr = session.beginTransaction();
Person p = (Person) session.get(Person.class, 1); // 獲取 System.out.println(person.getPhoto()); OutputStream out = new FileOutputStream("D:/apple.png"); out.write(person.getPhoto()); out.close(); tx.commit(); session.close();}
相關推薦
將檔案儲存到資料庫(以二進位制流的形式)
Hibernate方法 HibernateUtils.java package yang.fang.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import
C#將檔案上傳、下載(以二進位制流儲存到資料庫)
1、將檔案以二進位制流的格式寫入資料庫 首先獲得檔案路徑,然後將檔案以二進位制讀出儲存在一個二進位制陣列中,與資料庫建立連線,在SQL語句中將二進位制陣列賦值給相應的引數,完成向資料庫中寫入檔案的操作 /// 將檔案流寫入資料庫 /// </s
JDBC呼叫儲存過程(以MySQL為例)
-- drop procedure test_proc; DELIMITER // CREATE PROCEDURE test_proc(out p_out int, inout p_inout int, in p_in int) BEGIN set p_out=200000002
JDBC連線資料庫(以mysql為例)
步驟: 1、在專案裡面配置驅動(右擊專案->built path->configure built path->libraries->add External JARs...),新增資料庫的驅動jar包(本例mysql驅動包v5.0.8)網上下載驅動
磁碟分割槽大小顯示(以對數的形式)
var pts: array [0..8] of single= (111, 399, 90, 300, 5, 5, 1, 2, 18);//單位:M procedure TForm1.Button1Click(Sender: TObject); var dw: I
C#從資料庫中讀取二進位制流並生成檔案
下面以圖片檔案為例加以說明。從資料庫表 “圖片儲存”中讀取ID為1的圖片資料並生成圖片檔案。 MySqlConnection conn = new MySqlConnection("Server=localhost;Database=test;charset=utf8;U
httpclient4.5如何以二進位制流的形式上傳檔案
本人在做介面測試的過程中,遇到了上傳檔案的測試。由於之前沒有測試過流上傳,導致花費了一些時間,還有就是網上關於httpclient4.5的資料和其他版本資料混在一起,也浪費了一些時間。經過測試終於可用了。暫時沒有進一步封裝,下面分享一些主要的程式碼。其實 httpclien
log4j 日誌檔案儲存資料庫的解決方案一(配置檔案中寫sql語句)
log4jmysql.properties: log4j.rootLogger=ERROR,console,db log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.
將Excel檔案匯入資料庫(POI+Excel+MySQL+jsp頁面匯入)第一次優化
本篇文章是根據我的上篇部落格,給出的改進版,由於時間有限,僅做了一個簡單的優化。2018年4月1日,新增下載地址連結:點選開啟原始碼下載地址十分抱歉,這個連結地址沒有在這篇文章上公佈出來。希望不是很晚。上篇文章的是這樣的一個數據流向:瀏覽器端開啟上傳頁面,選擇檔案,上傳,將e
CMakeList.txt在大型檔案應用(以前端為例)
基本的CMakeLists.txt並不難,主要有生成庫、生成執行檔案、連結二者以及找庫、找標頭檔案、生成執行檔案、連結庫和執行檔案兩種方法組成。 但是遇到大型庫的編寫,目標是生成一個新的大型庫myslam,生成執行檔案、連結二者。需要提前宣告生成執行檔案在bin,庫在lib中,並把OPenCV、
修改Jupyter預設檔案儲存地址(可靠)
修改Jupyter預設檔案儲存地址,有三個步驟,而網上很多答案只有兩個,結果儲存的檔案還在原來的地址,根本不起作用。迴歸正題,這裡以Anaconda 3 為例,執行系統是Win 7。 1、新增jupyter_notebook_config.py配置檔案 開啟Windows執行框,
前端接收資料流實現圖片預覽效果--ajax 請求二進位制流 圖片 檔案 XMLHttpRequest 請求並處理二進位制流資料 之最佳實踐
本文為轉載文章 原文連結:https://www.cnblogs.com/cdemo/p/5225848.html 首先要謝謝這位大神的無私貢獻!解決了我的問題也完美表達了我當時的心路歷程 ajax 請求二進位制流 圖片 檔案 XMLHttpRequest 請求並處理二進位制流資料 之最佳實踐
C# 用配置檔案連線資料庫(Winform和Webform)
Winform中用配置檔案連線資料庫 資料庫連線類YSqlHelper程式碼: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst
JAVA中IO流文字檔案的操作(BufferedWriter和BufferedReader 緩衝區)
JAVA 中 字元流的緩衝區 分為了BufferedWriter 和BufferedReader 先講BufferedWriter 作用: 緩衝區要結合流才可以使用,而且在流的基礎上對流的功能進行了增強。我們也可以說,在我們以後的實際運用之中,為了增強一下I
Arduino核心檔案分析(以Stm32duino為例)
這篇部落格主要是分析stm32duino的底層檔案結構,來分析stm32duino 的實現原理和它的基本框架。 使用的工具是Source Insight ,新建工程,新增原始碼路徑之後可以進行分析。 開啟工程原始碼的資料夾後,有四個資料夾,我們主要分
Java下載檔案(以TIM為例)
package download; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL;
清清楚楚地搭建MongoDB資料庫(以搭建4.0.4版本的副本集為例)
資料的目錄檔案層次設計 我們一般採用多例項的方式,而不是將所有的資料庫儘可能地放在一個例項中。 主要基於以下考慮: 1:不同業務線對應的資料庫放在不同的例項上,部分操作的運維時間容易協調等到。 2:相互獨立,減少相互干擾。不會因為某個業務的激增或某個開發Team的程式碼問題,拖累太多的
WINDOWS解決升級npm依賴包的bat檔案,將檔案儲存為bat格式字尾,放node專案目錄執行
@echo off & npm outdated --parseable --depth=0 >up.txt & for /f “delims=^” %%i in (up.txt) do ( for /f “delims=:” %%i in ("%%~ni") do (
下載,更新及刪除檔案(以圖片為例)
一、下載檔案 public void downloaddownfiles(HttpServletResponse response, String filepath) { try { File file = new File(filepath);//filepath為
遍歷資料夾中的檔案(以圖片為例)
需求:前臺頁面的圖片從伺服器中動態提取 思路:1.遍歷該圖片資料夾 2.將所有圖片名稱以List集合傳入前端頁面 3.前端遍歷,根據名稱讀取圖片 工具:ajax 方法:$.each(json,fu