Reading image with FileInputStream

Kevin Neibarger, modified 6 Years ago. Regular Member Posts: 105 Join Date: 2/2/18 Recent Posts
I'm trying to read an image from the images directory that does exist via the FileInputStream reader in java class and I'm receiving the "No such file or directory" exception. I've tried prepending the contextPath. I even pulled up the image on the contextPath in a browser - http://localhost:8080/<MY_PORTLET_CONTEXT>/images/do-not-call-registry.jpg, and it was displayed. 

I'm essentially trying to display an image in an Excel Spreadsheet with the Apache POI API. The following code throws the "File not found exception".  Any ideas? Attached is my directory structure.

private void addDNCImage(HSSFWorkbook workbook, HSSFSheet sheet, int colNum, int rowNum, ResourceRequest request) {
try {
String contextPath = request.getContextPath();
InputStream dnc_image = new FileInputStream(contextPath + "/images/do-not-call-registry-new.jpg");

byte[] bytes = IOUtils.toByteArray(dnc_image);
int my_picture_id = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
dnc_image.close();
HSSFPatriarch drawing = sheet.createDrawingPatriarch();
ClientAnchor my_anchor = new HSSFClientAnchor();
my_anchor.setCol1(colNum);my_anchor.setRow1(rowNum);
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
my_picture.resize();
} catch (IOException e) {e.printStackTrace();
}
}
thumbnail
Mohammed Yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi ,
You can access using current classloader something like below
ClassLoader classLoader = getClass().getClassLoader();
InputStream is = classLoader.getResourceAsStream("/company_logo.png");