`
jie2workjava
  • 浏览: 151129 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论
阅读更多
public class TestImage {
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://localhost:3306/hejietest";
	private static String username = "root";
	private static String password = "123";
	private static Connection conn ;
	public static void main(String[] args) throws Exception{
		TestImage ti = new TestImage();
//		ti.testHibernate();
		ti.testHibernateTo();
//		ti.testJdbc();
	}
	/**
	 * 测试jdbc
	 * @throws Exception
	 */
	public void testJdbc()throws Exception{
		Class.forName(driver);
		conn = DriverManager.getConnection(url,username,password);
		PreparedStatement psta = conn.prepareStatement("insert into testImage (name,image) values('testimage',?)");
		FileInputStream fis = this.getFileInputStream();
		psta.setBinaryStream(1,fis,fis.available());   //********关键步骤
		psta.executeUpdate(); 
		psta.close();
		
		PreparedStatement psta1 = conn.prepareStatement("select image from testImage where id=1");
		ResultSet rs = psta1.executeQuery();
		byte[] bt = new byte[100];
		int len = 0;
		FileOutputStream fos = new FileOutputStream("D:\\web_workspace\\myeclipseworkspace\\result\\src\\f1950956660.jpg");
		while(rs.next()){
			Blob b = rs.getBlob(1);                //********关键步骤
			InputStream is = b.getBinaryStream(); //********关键步骤
			while((len=is.read(bt))!=-1){
			         fos.write(bt);
			}
			
		}
		fos.flush();
		fos.close();
		psta1.close();
	}

	/**
	 * 测试hibernate
	 * @throws Exception
	 */
	public void testHibernate()throws Exception{
		Session sess = HibernateSessionFactory.getSession();
		Transaction tr = sess.beginTransaction();
		Query q = sess.createQuery("from Testimage");
		List list = q.list();
		tr.commit();
		Testimage ti = (Testimage) list.get(0);
		
		System.out.println(ti.getImage());
		Blob b = ti.getImage();// //********关键步骤
		InputStream is = b.getBinaryStream(); //********关键步骤
		String dir = "D:\\web_workspace\\myeclipseworkspace\\result\\src\\h.jpg";
		File f = new File(dir);
		FileOutputStream os = new FileOutputStream(f);
		System.out.println(is.available());
		byte[] bt= new byte[is.available()]; //********关键步骤
		is.read(bt);
		os.write(bt);
		os.close();
		is.close();
	}
	public void testHibernateTo()throws Exception{
		String dir = "D:\\web_workspace\\myeclipseworkspace\\result\\src\\h.jpg";
		Testimage ti = new Testimage();
		ti.setName("hibernate");
		FileInputStream fis = new FileInputStream(dir);
		byte[] bt = new byte[fis.available()];
		fis.read(bt);
		ti.setImage(Hibernate.createBlob(bt));/ //********关键步骤
		
		Session sess = HibernateSessionFactory.getSession();
		Transaction tr = sess.beginTransaction();
		
		sess.save(ti);
		sess.flush();
//		FileInputStream fis = new FileInputStream(dir);
//		byte[] bt = new byte[fis.available()];
//		Blob b = ti.getImage();
////		OutputStream os = b.setBinaryStream(fis.available());
//		OutputStream os = b.(fis.available());
//		
//		fis.read(bt);
//		os.write(bt);
//		os.close();
//		fis.close();
		tr.commit();
		
	}
	
	/**
	 * 获得文件
	 * @return
	 * @throws Exception
	 */
	public FileInputStream getFileInputStream()throws Exception{
		String dir = "D:\\web_workspace\\myeclipseworkspace\\result\\src\\1950956660.jpg";
		File f = new File(dir);
		FileInputStream fis = new FileInputStream(f);
		return fis;
	}
}

 
import java.sql.Blob;

/**
 * Testimage entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class Testimage implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private Blob image;

	// Constructors
 

 

分享到:
评论

相关推荐

    image_to_db.rar_visual c

    在“image_to_db.rar_visual c”这个项目中,我们关注的是如何使用Visual C++来处理图像数据并将它们存储到数据库中。Visual C++是一种强大的开发环境,它允许程序员创建桌面应用程序,并且可以通过其MFC(Microsoft...

    f_import_image_to_db.zip_PowerBuilder

    标题 "f_import_image_to_db.zip_PowerBuilder" 暗示了这是一个与使用PowerBuilder开发的数据库图像导入功能相关的代码库。PowerBuilder是一款强大的、基于事件驱动的编程环境,主要用于构建企业级应用程序,特别是...

    Image to SQL.txt

    if DB_ID('UserImage') is not null drop database UserImage create database UserImage go ``` 此段代码首先使用`master`数据库,并检查是否存在名为`UserImage`的数据库。如果存在,则删除它,之后创建新的`User...

    Image to Access.txt

    根据提供的文件信息,本文将详细解释与 "Image to Access.txt" 相关的关键知识点,包括如何使用 C# 和 ADO.NET 技术处理图像数据,并将其存储在 Access 数据库中。 ### 一、项目背景 该代码片段属于一个名为 `...

    Image2StyleGAN++翻译1

    例如,PSNR从20dB大幅提高到45dB。这项技术可以广泛应用于图像重建、图像修复、图像交叉等领域。 Image2StyleGAN++的局部嵌入技术可以实现高质量的局部编辑。例如,图像的局部style transfer、图像编辑使用...

    db2认证 000-611V8.02.pdf

    Restore the database from a backup image that was taken before the table was dropped; run the ROLLFORWARD DATABASE command with the RECOVERDROPPEDTABLE option to restore the database and recover the ...

    PSNR.rar_ psnr color image _The Image_image psnr_noisy image

    标题中的"PSNR.rar_ psnr color image _The Image_image psnr noisy image"表明了这个压缩包文件主要涉及图像处理领域的一个重要概念——峰值信噪比(Peak Signal-to-Noise Ratio,简称PSNR),特别是在彩色图像上的...

    AT45DB321D-SU.pdf

    - **Description**: The AT45DB321D is a 2.7-volt, serial interface sequential access Flash memory device that is well-suited for various digital voice, image, program code, and data storage ...

    vi2.zip_MSE image_PSNR function_decomposition_image mse_mse

    (Please note that select 256X256 image for better result.) Then compression can performed, PERFL2 give compression score. Then reconstruction can be performed. Each decompsition we can choose ...

    PSNR.rar_The Image

    在图像处理和数字信号处理领域,峰值信噪比(Peak Signal-to-Noise Ratio,简称PSNR)是一个衡量图像质量的重要指标。它用于评估经过处理的图像与原始图像之间的差异,通常用分贝(dB)来表示。PSNR值越高,表示处理...

    BlobImage:将图像加载为 blob 并将它们存储在 bowser 的缓冲区中

    script type =" type/javascript " src =" /path/to/script/blobimage.js " > </ script > 应用程序接口 新的 BlobImage(url) var blobImage = new BlobImage ( '...

    time_freq_plot.zip_Through the Window_signal dB_stepped Frequenc

    This function creates a time-vs-frequency plot image for analyzing a signal s time-evolving frequency content. The signal is x, an analysis window of window_size samples is stepped through the time ...

    image.rar_MSE image_PSNR的代码实现_image mse_psnr of an image_图像均方误差

    均方误差(Mean Square Error,MSE)和峰值信噪比(Peak Signal-to-Noise Ratio,PSNR)是两种常用的量化图像质量的方法。此外,熵作为信息论中的概念,也在图像分析中起到关键作用。下面将详细介绍这些概念以及如何...

    Flattern.zip

    后续会把bilibili上面把原视频放上去。 Laravel Vue js Blog Project Here are the things you ...* How to Image intervation with Laravel (Used for image resize) * How to Use lodash * And much more...

    SNR.rar_P frames_PSNR_i frame_snr image_video image

    标题中的"SNR.rar_P frames_PSNR_i frame_snr image_video image"表明这是一个关于视频编码和质量评估的实验,重点在于I帧和P帧的SNR(Signal-to-Noise Ratio,信噪比)可伸缩编码。SNR是衡量信号质量和纯净度的重要...

    图像信噪比和峰值信噪比计算

    在图像处理领域,信噪比(Signal-to-Noise Ratio, SNR)和峰值信噪比(Peak Signal-to-Noise Ratio, PSNR)是衡量图像质量的重要指标。它们可以帮助我们评估图像在经过处理(如压缩、传输或恢复)后,保留原始信息的...

    Wavelet-Image-Compression-Reconstruction

    5. **性能评估**:项目中提到的"PERFL2"可能是性能指标,通常用均方误差(Mean Squared Error, MSE)或峰值信噪比(Peak Signal-to-Noise Ratio, PSNR)来衡量重构图像与原始图像的差异。L2范数(L2-norm)也常用于...

    JavaScript-Canvas-to-Blob.zip

    - 存储在浏览器的IndexedDB或Web Storage中。 - 使用FormData对象上传到服务器。 - 创建一个URL(通过URL.createObjectURL(blob)),然后设置为img标签的src属性,实现在页面上预览图片。 在实际应用中,Canvas to ...

    node-socket-imageServer:一个简单的图像服务器,它从 Mysql DB 加载图像并在客户端连接 socket.io 时发送它

    一个简单的图像服务器,它从 Mysql DB 加载图像并在客户端连接 socket.io 时发送它 在本地运行 git clone git@github.com:rnayathodan/node-socket-imageServer.git cd node-socket-imageServer npm install mysql -...

    wpsnr.m.zip_NOISE_wpsnr

    Typical quoted PSNR figures are in the range +25 to +35dB. The syntax for this file is PSNR(A,B), where A and B are MATLAB Intensity Images, with matrix-elements in the interval [0,1] The function ...

Global site tag (gtag.js) - Google Analytics