一、读取文件到byte数组
File file = new File("D://HD.jpg");
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
bin = null;
bin = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
while (bin.read(buffer) > 0) {
bos.write(buffer);
}
bin.close();
bos.close();
byte[] fileBytes = bos.toByteArray();
成功样式
二、读取文件到String
File file = new File("D://HD.jpg");
// 将文件转换成String
String fileToString = FileUtils.readFileToString(file, "UTF-8");
成功样式
三、缩略图
缩略图的话,需要导入依赖thumbnailator,Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好,功能十分强大,可以进行缩略、旋转、按比例缩略、裁剪等操作,最后通过BufferedImage也是保存为byte[] 类型数据进行保存