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进行缩略图的处理

你可能感兴趣的:(后端开发)