ORACLE———BLOB字段的存储于读取

先来实体类

package com.sola.bean;

import java.sql.Blob;

public class Test {

	private Integer id;
	private String name;
	private byte [] blob;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public byte[] getBlob() {
		return blob;
	}
	public void setBlob(byte[] blob) {
		this.blob = blob;
	}


}

Controller层

	@GetMapping("/idtestsave")
	@ApiOperation(value = "测试插入后获取id")
	public boolean idtestsave(Test test){
		
		return service.idtestsave(test);
			
	}

Service层

public boolean idtestsave(Test test) {
		// TODO Auto-generated method stub
		try {
			
			File file = new File("E:\\io\\itest.jpeg");
			
			byte[] fileToByte = IOUtils.getFileToByte(file);
			
			test.setBlob(fileToByte);			
			
			empMapper.idtestsave(test);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return false;
	}

IOUtils工具类

package com.sola.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {

	public static byte[] getFileToByte(File file) {
        byte[] by = new byte[(int) file.length()];
        InputStream is =null;
        try {
        	is = new FileInputStream(file);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally {
        	try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        return by;
    }
}

Dao层

public void idtestsave(Test test);

Mapper层

	<insert id="idtestsave" parameterType="com.sola.bean.Test">
		insert into IDTEST(name,blob) values(#{name,jdbcType=VARCHAR},#{blob,jdbcType=BLOB})
	</insert>

下面是读取

Controller层

	@GetMapping("/blobread")
	@ApiOperation(value = "blob读取测试")
	public Map<String,Object> readBlob(String id){
		
		return service.readBlob(id);
			
	}

Service层

 /** 读取blob测试 */
	public Map<String, Object> readBlob(String id) {
		
		Test readBlob = empMapper.readBlob(id);
		
		byte[] blob = readBlob.getBlob();
		
		FileOutputStream os =null;
		try {
			os = new FileOutputStream("E:\\io\\"+readBlob.getId()+"-"+readBlob.getName()+".jpeg");
			
			os.write(blob);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			try {
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

说明:Serivce内操作只是在把读取出来二blob字段存到本地,没有返回到前端

Dao层

public Test readBlob(@Param("id")String id);

Mapper层

	<select id="readBlob" parameterType="String" resultType="com.sola.bean.Test">
		select * 
		  from IDTEST t 
		  <where>
		  t.id = #{id}
		  </where>
	</select>
锵锵锵!!!示例到此结束

猜你喜欢

转载自blog.csdn.net/jiulanhao/article/details/80829590