Page 1 of 1

IO Exception permission denied while trying to add image

PostPosted: Wed Mar 21, 2018 7:14 pm
by AnilV
Hi Jason,

When i am trying to add an image to document using docx4j, getting the below exception.
java.io.IOException: Permission denied

Looks like 'createImageInline' method is throwing the exception. The bytes which are passed to addImageToPackage method are correct and the image is of type JPG.
In many of our systems this is working but only in one system we are facing an issue. Could you please let us know what could be the issue.


Using the below code to add image to document.

private void addImageToPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage wordMLPackage, byte[] bytes)
throws Exception {
org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage imagePart = org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);

int docPrId = 1;
int cNvPrId = 2;
org.docx4j.dml.wordprocessingDrawing.Inline inline = imagePart.createImageInline("Filename hint",
"Alternative text", docPrId, cNvPrId, false);

org.docx4j.wml.P paragraph = addInlineImageToParagraph(inline);

wordMLPackage.getMainDocumentPart().addObject(paragraph);
}

private org.docx4j.wml.P addInlineImageToParagraph(org.docx4j.dml.wordprocessingDrawing.Inline inline) {
// Now add the in-line image to a paragraph
org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P paragraph = factory.createP();
org.docx4j.wml.R run = factory.createR();
paragraph.getContent().add(run);
org.docx4j.wml.Drawing drawing = factory.createDrawing();
run.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return paragraph;
}

Re: IO Exception permission denied while trying to add image

PostPosted: Thu Mar 22, 2018 8:11 pm
by jason
Possibly this code?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        public static BinaryPartAbstractImage createImagePart(
                        OpcPackage opcPackage,
                        Part sourcePart, byte[] bytes) throws Exception {
                               
                // Whatever image type this is, we're going to need
                // to know its dimensions.
                // For that we use ImageInfo, which can only
                // load an image from a URI.
               
                // So first, write the bytes to a temp file            
                File tmpImageFile = File.createTempFile("img", ".img");
               
                FileOutputStream fos = new FileOutputStream(tmpImageFile);
                fos.write(bytes);
                fos.close();
        log.debug("created tmp file: " + tmpImageFile.getAbsolutePath());
                               
                ImageInfo info = ensureFormatIsSupported(tmpImageFile, bytes, true);
 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4

Re: IO Exception permission denied while trying to add image

PostPosted: Thu Mar 29, 2018 9:33 pm
by AnilV
Hi Jason,

User has the permission to write the files into that folder. But why is he still getting permission denied exception. Is there something else we are missing here?

Re: IO Exception permission denied while trying to add image

PostPosted: Fri Mar 30, 2018 7:20 am
by jason
Where is the error coming from (class, line number)?

Do you have a stack trace?