#复盘#
情况说明
我在使用java api的方式将非结构化数据(六张图片)上传到HBase上的时候,出现了报错Exception in thread "main" javax.imageio.IIOException: Can't read input file!报错。
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at org.example.dao.HBaseDao.setPicture(HBaseDao.java:104)
at org.example.dao.HBaseDao.UploadCondition(HBaseDao.java:89)
at org.example.test.TestOil.main(TestOil.java:23)
我的代码如下:
public static void UploadCondition() throws IOException {
Connection connection = ConnectionFactory.createConnection(Constants.CONFIGURATION);
Table condTable = connection.getTable(TableName.valueOf(Constants.CONDITION_TABLE));
for(int i=1;i<6;i++){
String name = "picture"+i+".jpg";
// System.out.println(name);
String rowKey = "picture"+i;
// System.out.println(rowKey);
String Path = "D:\\Project\\bishe\\demo\\HDFSClient\\src\\main\\java\\org\\example\\pictures\\"+name;
// System.out.println(Path);
ArrayList L = setPicture(Path);
Put condPut = new Put(Bytes.toBytes(rowKey));
condPut.addColumn("name".getBytes(),"info".getBytes(),L.get(0).toString().getBytes());
System.out.println(Path);
condTable.put(condPut);
}
condTable.close();
}
public static ArrayList setPicture(String name) throws IOException {
ArrayList<String> list = new ArrayList<>();
File file = new File(name);
BufferedImage input = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(input,"jpg",baos);
list.add(encoder.encodeBuffer(baos.toByteArray()));
return list;
}
上述代码已经对encoder进行了定义,目的是将pictures文件夹下的6张图片(picture1 - 6)上传到HBase - oil:condition 表(列族分别为 name、info)中。该部分代码作为方法进行引用。
好的,接下来开始排雷!
排雷思路
1、检查文件路径&文件是否确实存在
在setPicture方法上添加部分代码,利用File.exists()
和File.canRead()
方法检查文件是否存在以及是否可读。
public static ArrayList<String> setPicture(String filePath) throws IOException {
System.out.println("尝试读取图片文件: " + filePath);
ArrayList<String> list = new ArrayList<>();
File file = new File(filePath);
if (file.exists()) {
System.out.println("文件存在");
if (file.isFile()) {
System.out.println("是一个文件");
if (file.canRead()) {
System.out.println("可读");
// 尝试读取文件
} else {
System.out.println("不可读");
}
} else {
System.out.println("不是一个文件");
}
} else {
System.out.println("文件不存在");
}
if (!file.exists()) {
throw new FileNotFoundException("文件不存在: " + filePath);
}
if (!file.isFile()) {
throw new IllegalArgumentException("路径不是一个文件: " + filePath);
}
if (!file.canRead()) {
throw new IOException("无法读取文件: " + filePath);
}
BufferedImage input = ImageIO.read(file);
if (input == null) {
throw new IOException("无法读取图像文件: " + filePath);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(input, "jpg", baos);
list.add(encoder.encodeBuffer(baos.toByteArray())); // 假设encoder是已经初始化的Base64编码器
return list;
}
我的返回结果如下:
尝试读取图片文件: D:\Project\bishe\demo\HDFSClient\src\main\java\org\example\pictures\picture1.jpg
文件存在
是一个文件
可读
Exception in thread "main" java.io.IOException: 无法读取图像文件: D:\Project\bishe\demo\HDFSClient\src\main\java\org\example\pictures\picture1.jpg
at org.example.dao.HBaseDao.setPicture(HBaseDao.java:131)
at org.example.dao.HBaseDao.UploadCondition(HBaseDao.java:93)
at org.example.test.TestOil.main(TestOil.java:23)
好好好,可读又读不出来是吧,继续排雷。
2、确保有足够的权限来读取该文件
前往pictures文件夹下,右键 属性 - 安全,查看用户中是否有本机。(一般都有权限)
3、查看 ImageIO
支持的格式列表,并确认你的文件是否属于这些格式之一
查了一下,ImageIO支持的格式列表有Read:JPEG 2000, JPG, tiff, bmp, PCX, gif, WBMP, PNG, RAW, JPEG, PNM, tif, TIFF, wbmp, jpeg, jbig2, jpg, JPEG2000, BMP, pcx, GIF, png...我的图片格式是.jpg,无疑是没问题的。
最后
能排的都排完了,还是有问题,我就觉得非常奇怪了,突然想着,那我把i设置成2吧,看看第二张图片能不能上传。好好好,这一试发现,第二张上传了,第三张上传了,第四张上传了,第五张又传不上了。。。
搜嘎!原来是百度上下载图片的格式问题。我下的时候有些图片默认格式是.webp,我给它直接改了后缀,改成.jpg了。虽然电脑上的图片查看器也可以打开,IDEA也可以查看,但是这个上传就是不能读取。
#记录一下我神奇的debug之路