String str = session.getServletContext().getRealPath("/") + "img/other/kute1.PNG";
//获取图片的webapp路径
File file = new File(str);
byte[] b = new byte[1024];
byte[] fileByte = new byte[(int) file.length()];
try(BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));) {
while(inputStream.read(b)!=-1) {
org.apache.catalina.tribes.util.Arrays.contains(fileByte, 0, b, 0, 1024);
//使用apache提供的Arrays连接字节数组
};
在存储进mysql blob字段时,出现Data truncation: Data too long for column ‘CONTENT’ at row 1
原因是blob装不下,换成long blob就行了
做到这一步之后,突然发现思路有问题,我在数据库中存储了图片,打算在项目中取出图片,突然发现不用经过数据库,直接读写就可以了,然后在数据库中存储相对路径就好了