1. 程式人生 > >Mysql使用Blob資料型別存取圖片

Mysql使用Blob資料型別存取圖片

一般情況下圖片的儲存在hdfs上,資料庫只儲存圖片的路徑,特殊情況採用這個方案
資料庫方面:
這裡寫圖片描述
實體方面採用byte陣列進行存取:

 private byte[] img;

我在本地磁碟放了一張圖片,現在進行讀取:

public String save(){
//建立實體
DashboardBoard board = new DashboardBoard();
byte[] inputStream2ByteArray;
             try {
             inputStream2ByteArray = InputStream2ByteArray("D:\\ceshi.png"
); } catch (IOException e) { e.printStackTrace(); } board.setImg(exportBoard); //呼叫儲存方法 boardDao.save(board); //具體業務~略 } //主要的工具方法 public byte[] InputStream2ByteArray(String filePath) throws IOException { InputStream in
= new FileInputStream(filePath); byte[] data = toByteArray(in); in.close(); return data; } public byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int
n = 0; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); } return out.toByteArray(); }

通過以上方式可以把圖片以二進位制的方式儲存到資料庫中,親測,有效。

圖片的顯示

前臺頁面使用img標籤進行圖片的顯示
<img src="localhost:8080/xxx/getBoardDataImg.do?id=xx&width=150&height=150" />

後臺程式碼:

@RequestMapping(value = "/getBoardDataImg")
    @ResponseBody
    public String ceshi(@RequestParam(name = "id") Long id ,@RequestParam(name = "width") int width,@RequestParam(name = "height") int height,  HttpServletResponse response,HttpServletRequest request) throws IOException{
        DashboardBoard board = boardDao.getBoard(id);
        byte[] data = board.getImg();
        //獲取縮圖
        data = boardService.scaleImage(data, width, height);  
        response.setContentType("image/jpeg");  
        response.setCharacterEncoding("UTF-8");  
        OutputStream outputSream = response.getOutputStream();  
        InputStream in = new ByteArrayInputStream(data);  
        int len = 0;  
        byte[] buf = new byte[1024];  
        while ((len = in.read(buf, 0, 1024)) != -1) {  
            outputSream.write(buf, 0, len);  
        }  
        outputSream.close();  
        return null;
    }

}
/**
     * 獲取縮圖
     * @throws IOException
     */
    public  byte[] scaleImage(byte[] data, int width, int height) throws IOException {
        BufferedImage buffered_oldImage = ImageIO.read(new ByteArrayInputStream(data));
        int imageOldWidth = buffered_oldImage.getWidth();
        int imageOldHeight = buffered_oldImage.getHeight();
        double scale_x = (double) width / imageOldWidth;
        double scale_y = (double) height / imageOldHeight;
        double scale_xy = Math.min(scale_x, scale_y);
        int imageNewWidth = (int) (imageOldWidth * scale_xy);
        int imageNewHeight = (int) (imageOldHeight * scale_xy);
        BufferedImage buffered_newImage = new BufferedImage(imageNewWidth, imageNewHeight, BufferedImage.TYPE_INT_RGB);
        buffered_newImage.getGraphics().drawImage(
                buffered_oldImage.getScaledInstance(imageNewWidth, imageNewHeight, BufferedImage.SCALE_SMOOTH), 0, 0,
                null);
        buffered_newImage.getGraphics().dispose();
        ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
        ImageIO.write(buffered_newImage, "jpeg", outPutStream);
        return outPutStream.toByteArray();
    }

}

使用流展示圖片到前端,通過設定width和height進行縮圖的處理