Anonymous
Java.lang.outofMemoryError: Java Heap Space während Bytea Download
Post
by Anonymous » 26 Aug 2025, 11:31
Ich verwende diesen Code, um Bytea -Objekt von postgresql herunterzuladen: < /p>
Code: Select all
public void initFileDBData() throws SQLException, IOException
{
if (ds == null)
{
throw new SQLException("Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null)
{
throw new SQLException("Can't get database connection");
}
PreparedStatement ps = null;
try
{
conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT * FROM PROCEDURE_FILES WHERE ID = ?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
String file_name = rs.getString("FILE_NAME");
InputStream binaryStreasm = rs.getBinaryStream("FILE");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentLength(binaryStreasm.available());
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\"");
byte[] buf;
buf = new byte[binaryStreasm.available()];
int offset = 0;
int numRead = 0;
while ((offset < buf.length) && ((numRead = binaryStreasm.read(buf, offset, buf.length - offset)) >= 0))
{
offset += numRead;
}
HttpServletResponse response
= (HttpServletResponse) FacesContext.getCurrentInstance()
.getExternalContext().getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file_name);
response.getOutputStream().write(buf);
response.getOutputStream().flush();
response.getOutputStream().close();
FacesContext.getCurrentInstance().responseComplete();
}
}
finally
{
if (ps != null)
{
ps.close();
}
conn.close();
}
}
< /code>
Aber wenn ich den Code herunterlade, erhalte ich java.lang.outofMemoryError: Java Heap Space in dieser Zeile: < /p>
buf = new byte[binaryStreasm.available()];
< /code>
Kann ich den Code irgendwie optimieren, um weniger Speicher zu konsumieren?public void initFileDBData() throws SQLException, IOException
{
if (ds == null)
{
throw new SQLException("Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null)
{
throw new SQLException("Can't get database connection");
}
PreparedStatement ps = null;
try
{
conn.setAutoCommit(false);
ps = conn.prepareStatement("SELECT *, octet_length(FILE) as file_length FROM PROCEDURE_FILES WHERE ID = ?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
String file_name = rs.getString("FILE_NAME");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentLength(rs.getInt("file_length"));
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\"");
HttpServletResponse response
= (HttpServletResponse) FacesContext.getCurrentInstance()
.getExternalContext().getResponse();
byte[] buffer = new byte[4096];
try (InputStream input = rs.getBinaryStream("FILE");
OutputStream output = response.getOutputStream())
{
int numRead = 0;
while ((numRead = input.read(buffer)) != -1)
{
output.write(buffer, 0, numRead);
}
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + file_name);
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
response.getOutputStream().close();
FacesContext.getCurrentInstance().responseComplete();
}
}
finally
{
if (ps != null)
{
ps.close();
}
conn.close();
}
}
1756200673
Anonymous
Ich verwende diesen Code, um Bytea -Objekt von postgresql herunterzuladen: < /p> [code]public void initFileDBData() throws SQLException, IOException { if (ds == null) { throw new SQLException("Can't get data source"); } Connection conn = ds.getConnection(); if (conn == null) { throw new SQLException("Can't get database connection"); } PreparedStatement ps = null; try { conn.setAutoCommit(false); ps = conn.prepareStatement("SELECT * FROM PROCEDURE_FILES WHERE ID = ?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); while (rs.next()) { String file_name = rs.getString("FILE_NAME"); InputStream binaryStreasm = rs.getBinaryStream("FILE"); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentLength(binaryStreasm.available()); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\""); byte[] buf; buf = new byte[binaryStreasm.available()]; int offset = 0; int numRead = 0; while ((offset < buf.length) && ((numRead = binaryStreasm.read(buf, offset, buf.length - offset)) >= 0)) { offset += numRead; } HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + file_name); response.getOutputStream().write(buf); response.getOutputStream().flush(); response.getOutputStream().close(); FacesContext.getCurrentInstance().responseComplete(); } } finally { if (ps != null) { ps.close(); } conn.close(); } } < /code> Aber wenn ich den Code herunterlade, erhalte ich java.lang.outofMemoryError: Java Heap Space in dieser Zeile: < /p> buf = new byte[binaryStreasm.available()]; < /code> Kann ich den Code irgendwie optimieren, um weniger Speicher zu konsumieren?public void initFileDBData() throws SQLException, IOException { if (ds == null) { throw new SQLException("Can't get data source"); } Connection conn = ds.getConnection(); if (conn == null) { throw new SQLException("Can't get database connection"); } PreparedStatement ps = null; try { conn.setAutoCommit(false); ps = conn.prepareStatement("SELECT *, octet_length(FILE) as file_length FROM PROCEDURE_FILES WHERE ID = ?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); while (rs.next()) { String file_name = rs.getString("FILE_NAME"); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentLength(rs.getInt("file_length")); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + file_name + "\""); HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); byte[] buffer = new byte[4096]; try (InputStream input = rs.getBinaryStream("FILE"); OutputStream output = response.getOutputStream()) { int numRead = 0; while ((numRead = input.read(buffer)) != -1) { output.write(buffer, 0, numRead); } } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + file_name); response.getOutputStream().write(buffer); response.getOutputStream().flush(); response.getOutputStream().close(); FacesContext.getCurrentInstance().responseComplete(); } } finally { if (ps != null) { ps.close(); } conn.close(); } } [/code]