簡體   English   中英

javax.imageio.IIOException:寫入PNG文件時發生I / O錯誤

[英]javax.imageio.IIOException: I/O error writing PNG file

我正在使用Jpedal工具將PDF轉換為圖像。 當PDF頁面的數量很大並且我們對其進行處理以進行轉換時,tomcat將停止並引發Exception-

  javax.imageio.IIOException: I/O error writing PNG file.

任何人都可以幫忙。

public boolean createPDF2ImageTask(String sourcePDFAbsPath, String destinationImageAbsPath, Float scalingFactor, String fileFormat, int softLimitInKB) throws Exception
    { 

        System.setProperty("org.jpedal.flattenForm","true");
        logger.info("createPDF2ImageTask ( sourcePDFAbsPath = "+sourcePDFAbsPath+" , destinationImageAbsPath = "+destinationImageAbsPath+ ", scalingFactor = "+scalingFactor+ " , fileFormat = "+fileFormat+ " softLimitInKB ="+softLimitInKB );
        boolean status = true;
        Float newScalingFactor;
        int sizeOfImageInKB;

        //PdfDecoder object provides the conversion 

        PdfDecoderServer decoder = null;
        Map mapValues = null;
        BufferedImage imageToSave = null;
        BufferedOutputStream bufferedOutputStream = null;
        long startTime = System.currentTimeMillis();
        try
        {

            Helper.deleteFile(destinationImageAbsPath);

            //mappings for non-embedded fonts to use
            FontMappings.setFontReplacements();

            decoder = new PdfDecoderServer(true);
            decoder.openPdfFile(sourcePDFAbsPath);


            mapValues = new HashMap();


            mapValues.put(JPedalSettings.EXTRACT_AT_BEST_QUALITY_MAXSCALING, 2);

            //alternatively secify a page size (aspect ratio preserved so will do best fit)
            //set a page size (JPedal will put best fit to this)
            PdfPageData pageData = decoder.getPdfPageData();
            int width = (int)(scalingFactor*pageData.getCropBoxWidth(1));
            int height = (int)(scalingFactor*pageData.getCropBoxHeight(1));
            logger.info("width = "+ width + "   height= "+height);          
            mapValues.put(JPedalSettings.EXTRACT_AT_PAGE_SIZE, new String[]{String.valueOf(width),String.valueOf(height)});

            //which takes priority (default is false)
            mapValues.put(JPedalSettings.PAGE_SIZE_OVERRIDES_IMAGE, Boolean.TRUE);

            PdfDecoderServer.modifyJPedalParameters(mapValues);                              

            /////////////////////////////////////////////////////////////////////////////////////

            try
            {
                imageToSave = decoder.getPageAsHiRes(1, null, false);
                decoder.flushObjectValues(true);
                if(imageToSave != null)
                {
                    logger.info("Start saving image as a file");
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(destinationImageAbsPath)));

                    ImageIO.write(imageToSave, fileFormat, bufferedOutputStream);

                }
                else
                {
                    throw new Exception("imageToSave is null, Exception in extractPageAsImage ");
                }
            }
            catch(Exception e)
            {
                logger.error("Exception in extractPageAsImage :: "+e);
                logger.error("Exception stack trace in extractPageAsImage :: ",e);
                throw new Exception("Exception in extractPageAsImage :: "+e);
            }

拋出異常-extractPageAsImage :: javax.imageio.IIOException中的異常:寫入PNG文件時發生I / O錯誤!

也許您可以嘗試通過增加Tomcat實例的內存來解決此問題。 在Unix平台上,可以通過在setenv.sh添加一些Java選項來實現:

export JAVA_OPTS="$JAVA_OPTS -Xms128m -Xmx2048m -XX:MaxPermSize=512m"

或在Windows中的setenv.bat文件中:

set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx2048m -XX:MaxPermSize=512m" 

請注意,以上示例中的數字僅是示例,因為它們取決於您平台上可用的內存。

有關如何在平台上配置Tomcat的更多詳細信息,請參考Tomcat的RUNNING.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM