讀取jar包中圖片文件,並轉換爲BufferImage

jar包中的資源讀取不到,真心難過,後來發現可以通過流的方式把資源讀取出來(包括.class本身)


//讀取jar包中的資源,但是讀到的是InputStream
InputStream 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();
		}
	}

發佈了20 篇原創文章 · 獲贊 7 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章