如何在java中正确加载BufferedImage?

好的,所以我一直在试图使用这个代码加载一个BufferedImage:

URL url = this.getClass().getResource("test.png"); BufferedImage img = (BufferedImage) Toolkit.getDefaultToolkit().getImage(url); 

当我运行它时,这给了我一个types转换错误,所以如何正确加载BufferedImage?

使用ImageIO.read()来代替:

 BufferedImage img = ImageIO.read(url); 
 BufferedImage img = null; try { img = ImageIO.read(new File("D:\\work\\files\\logo.jpg")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }