读取jar包中图片文件,并转换为BufferImage

来源:互联网 发布:mp3购买推荐淘宝网 编辑:程序博客网 时间:2024/06/05 17:29

jar包中的资源读取不到,真心难过,后来发现可以通过流的方式把资源读取出来(包括.class本身)


//读取jar包中的资源,但是读到的是InputStreamInputStream is=this.getClass().getResourceAsStream("/Plugin/ComUI/Resources/blupin.png");   
//接下来可以转为file,参照最下面的方法
//这里我要处理图片,所以直接就读取出来
BufferedImage img = ImageIO.read(is);


这个是一个InputStream转换为file文件的方法。

public void inputstreamtofile(InputStream ins,File file) {try {OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();} catch (Exception e) {e.printStackTrace();}}

0 0
原创粉丝点击