Java.lang.outofmemoryError: Java Heap Space во время загрузки ByteaJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Java.lang.outofmemoryError: Java Heap Space во время загрузки Bytea

Сообщение Anonymous »

Я использую этот код для загрузки объекта Bytea из postgresql: < /p>

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>

Но когда я начинаю загружать код, я получаю java.lang.outofmemoryerror: пространство кучи Java на этой строке: < /p>

buf = new byte[binaryStreasm.available()];
< /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 *, 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();
}
}


Подробнее здесь: https://stackoverflow.com/questions/370 ... a-download
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Java.lang.outofmemoryError: Java Heap Space во время загрузки Bytea
    Anonymous » » в форуме JAVA
    0 Ответы
    6 Просмотры
    Последнее сообщение Anonymous
  • Как справиться с ошибкой "java.lang.outofmemoryerror: Java Heap Space"?
    Anonymous » » в форуме JAVA
    0 Ответы
    12 Просмотры
    Последнее сообщение Anonymous
  • Java.lang.outofmemoryerror: java heap space ("-xmx1280m")
    Anonymous » » в форуме JAVA
    0 Ответы
    10 Просмотры
    Последнее сообщение Anonymous
  • Как справиться с ошибкой "java.lang.outofmemoryerror: Java Heap Space"?
    Anonymous » » в форуме JAVA
    0 Ответы
    3 Просмотры
    Последнее сообщение Anonymous
  • Способы исправить исключение в потоке "Main" java.lang.outofmemoryerror: Java Heap Space [закрыто]
    Anonymous » » в форуме JAVA
    0 Ответы
    30 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»