springboot獲取靜態資源

/**
	 *	獲取圖片	
	 * 	@param imgName
	 * 	@return
	 * @throws IOException 
	 */
	public Image getImageByName(String imgName) {
		ClassPathResource classPathResource = new ClassPathResource(imgName);
		try {
			byte[] byteArray = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
			return Image.getInstance(byteArray);
		} catch (IOException | BadElementException e1) {
			e1.printStackTrace();
		}
		
		return null;
	}


resources下文件路徑 /img/test.png

getImageByName("/img/test.png");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章