2013-10-15 66 views
2

我得到堆空間錯誤/內存不足異常。 我正在嘗試使用iText生成PDF並使用aspose api將PDF轉換爲jpg圖像。正在生成的PDF爲3頁,我將該PDF轉換爲逐頁圖像並將它們拼接成一個jpg圖像。這段代碼在我的本地開發機器上工作正常,但在移動到測試服務器時發生異常。Java堆空間錯誤/內存不足錯誤

我使用這裏面的代碼是:

public void silSignedPDF(AgreementBean agBean,String sourceTemplatePDFURL, Hashtable<String, String> val, String destinationPDFPath) throws IOException, DocumentException, SQLException{ 
String methodName = "silSignedPDF"; 
LogTracer.writeDebugLog(className, methodName, "Start"); 
String serverPath = System.getProperty("jboss.server.home.dir"); 
String sourceTemplatePDFURL1 = serverPath+AppConstants.PDL_Agreement_Template +"/Online_Installment_Agreement.pdf"; 
System.out.println("sourceTemplatePDFURL1 "+sourceTemplatePDFURL1); 

File f = new File(sourceTemplatePDFURL1); 
InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f)); 
File destinationFile = new File(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf"); 
PdfReader reader = new PdfReader(sourceTemplatePDFUrlStream); 
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
     destinationFile)); 
AcroFields form = stamper.getAcroFields(); 

Enumeration enumeration = val.keys(); 
// iterate through Hashtable val keys Enumeration 
while (enumeration.hasMoreElements()) { 
    String nextElement = (String) enumeration.nextElement(); 
    String nextElementValue = (String) val.get(nextElement); 
    form.setField(nextElement, nextElementValue); 
} 
stamper.setFormFlattening(true); 
stamper.close(); 

PdfConverter pdf = new PdfConverter(); 
pdf.bindPdf(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf"); 
try { 
    pdf.doConvert(); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

//set start and end pages 
pdf.setStartPage(1); 
pdf.setEndPage(1); 

//initialize conversion process 

//convert pages to images 
String suffix = ".jpg"; 
int imageCount = 1; 
while (pdf.hasNextImage()) 
{ 
    try { 
     pdf.getNextImage(destinationPDFPath+"/"+agBean.getDealNbr()+"_"+imageCount + suffix,ImageType.JPEG); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    imageCount++; 
} 
/* PDFImages pdfDoc = new PDFImages (destinationPDFPath+"/"+agBean.getDealNbr()+".pdf", null); 
for (int count = 0; count < pdfDoc.getPageCount(); ++count) 
{ 
    pdfDoc.savePageAsJPEG(count,destinationPDFPath+"/"+agBean.getDealNbr()+"_"+count + ".png", 150, 0.8f); 
}*/ 
File file1 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_1" + ".jpg"); 
File file2 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_2" + ".jpg"); 
File file3 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_3" + ".jpg"); 

BufferedImage img1 = ImageIO.read(file1); 
BufferedImage img2 = ImageIO.read(file2); 
BufferedImage img3 = ImageIO.read(file3); 

int widthImg1 = img1.getWidth(); 
int heightImg1 = img1.getHeight(); 
int heightImg2 = img2.getHeight(); 
int heightImg3 = img3.getHeight(); 

BufferedImage img = new BufferedImage(
widthImg1, 
heightImg1+heightImg2+heightImg3, 
BufferedImage.TYPE_INT_RGB); 

img.createGraphics().drawImage(img1, 0, 0, null); 
img.createGraphics().drawImage(img2, 0, heightImg1, null); 
img.createGraphics().drawImage(img3, 0, heightImg1+heightImg2, null); 

File final_image = new File(destinationPDFPath+"/"+agBean.getDealNbr() + ".jpg"); 
ImageIO.write(img, "png", final_image); 
file1.delete(); 
file2.delete(); 
file3.delete(); 
LogTracer.writeDebugLog(className, methodName, "End"); 
} 
+0

在您的機器上執行jvm並且服務器具有相同數量的內存? -Xmx512M –

+0

[Java堆空間內存不足]的可能的重複(http://stackoverflow.com/questions/6748432/java-heap-space-out-of-memory) – Ilya

回答

1

堆大小應該JVM的改變,但不要將其更改爲任何隨機數。應根據系統使用的內存修改堆大小。您可以檢查this進一步的規格。

0

Page to Image conversion使用Aspose.Pdf需要更多的內存比the default。使用至少512 MB內存(-Xmx512m)運行程序。

+0

是的,我增加了內存到512 MB,它的工作精細。 –