gpt4 book ai didi

java.lang.OutOfMemoryError : Java heap space at org. apache.xmlbeans.impl.store.Saver$TextSaver.resize

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:57 25 4
gpt4 key购买 nike

我试图将我的数据导出到 Excel 工作表中,它包含近 70000 行。我收到以下错误:

Exception in thread "http-bio-8765-exec-1" java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1700) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1303) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1190)

下面是用于编写Excel的代码:

public String getXLSWriter(ResultSet rs, String file_name)
{
String filepath="";
try {
//String filename=file_name.replace('/','_');
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(file_name);

XSSFRow rowhead = sheet.createRow((int) 0);
filepath=prop.getProperty("file_store_path")+file_name+timeStamp+".xlsx";

FileOutputStream fileOut = new FileOutputStream(filepath);
rowhead.createCell((int) 0).setCellValue("ID");
rowhead.createCell((int) 1).setCellValue("firstName");
rowhead.createCell((int) 2).setCellValue("LastName");

int i = 1;
while (rs.next()){
XSSFRow row = sheet.createRow((int) i);

row.createCell((int) 0).setCellValue(rs.getString("id"));
row.createCell((int) 1).setCellValue(rs.getString("first_name") );
row.createCell((int) 2).setCellValue(rs.getString("last_name"));


i++;
}

workbook.write(fileOut);
fileOut.close();
System.out.println("XLS done!!");
} catch (SQLException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

下面是我用于下载 Excel 的代码:

public void getDownloaded(String filepath,HttpServletResponse response)
{
try {
File file=new File(filepath);
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename="+file.getName());
FileInputStream in=new FileInputStream(file);
ServletOutputStream out=response.getOutputStream();
byte[] outputByte = new byte[4096];
//copy binary content to output stream
int length;
while((length=in.read(outputByte, 0, 4096)) != -1){
out.write(outputByte, 0, length);
}

in.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

启动java时不要一次读取所有数据或使用-Xmx选项来增加最大内存。检查内存泄漏并将此数据导入数据库以节省本地内存。

关于java.lang.OutOfMemoryError : Java heap space at org. apache.xmlbeans.impl.store.Saver$TextSaver.resize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com