Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Reading image with FileInputStream
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();
}
}
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();
}
}
Attachments:
Hi ,
You can access using current classloader something like below
You can access using current classloader something like below
ClassLoader classLoader = getClass().getClassLoader();
InputStream is = classLoader.getResourceAsStream("/company_logo.png");