Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

java - How can I get PNG Image from this code? Not able to view PNG Image in new Tab

This is the groovy file for viewing the document. Please help me with this.

def viewDocument(byte [] documentContentByte, def contentType){
        log.debug("Entering viewDocument")
        response.setContentType(contentType)
        OutputStream out = response.getOutputStream()
        out.write(documentContentByte)
        out.flush()
        out.close()
        log.debug("Exiting viewDocument")
    }

This is the code where I am passing the byte [] to the method. I am able to view the PDF Format but Not able to view the PNG Image.

def viewShippingLabelAsPDF() {
        log.debug("Entering viewShippingLabelAsPDF");
        try {
            ViewDocumentResponse resp = session.getAttribute("viewLabelResp")
            
            if(resp!=null && resp.documentContentByte!=null && resp.documentContent!=null && resp.documentContent.substring(0, 20).contains("PDF")){
                
                log.info("In PDF");
                def contentType = "application/pdf" 
                byte[] contentByte = resp.getDocumentContentByte();
                
                viewDocument(contentByte, contentType);
            }

            else if(resp!=null && resp.documentContentByte!=null && resp.documentContent!=null && resp.documentContentDecoded!=null && resp.documentContentDecoded.substring(0, 10).contains("PNG")){
                
                log.info("In PNG");
                def contentType = "image/png";
                byte[] contentByte = resp.getDocumentContentByte();
                
                viewDocument(contentByte, contentType)
            }
            else {
                log.info("Not selected any in ViewLabel..Abhishek")
            }
        } catch (Exception e) {
            log.error("Exception in viewShippingLabelAsPDF::" +e)
        }
        log.debug("Exiting viewShippingLabelAsPDF");
    }
    ```
The output is Blank page with black color in background.
question from:https://stackoverflow.com/questions/65546432/how-can-i-get-png-image-from-this-code-not-able-to-view-png-image-in-new-tab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...