que estoy tratando de publicar un archivo de vídeo/imagen grande desde el sistema de archivos local a una ruta http, pero me encuentro con un error de falta de memoria después de algún tiempo ...OutputStream OutOfMemoryError al enviar HTTP
aquí es el código
public boolean publishFile(URI publishTo, String localPath) throws Exception {
InputStream istream = null;
OutputStream ostream = null;
boolean isPublishSuccess = false;
URL url = makeURL(publishTo.getHost(), this.port, publishTo.getPath());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn != null) {
try {
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("PUT");
istream = new FileInputStream(localPath);
ostream = conn.getOutputStream();
int n;
byte[] buf = new byte[4096];
while ((n = istream.read(buf, 0, buf.length)) > 0) {
ostream.write(buf, 0, n); //<--- ERROR happens on this line.......???
}
int rc = conn.getResponseCode();
if (rc == 201) {
isPublishSuccess = true;
}
} catch (Exception ex) {
log.error(ex);
} finally {
if (ostream != null) {
ostream.close();
}
if (istream != null) {
istream.close();
}
}
}
return isPublishSuccess;
}
aquí está el error que estoy recibiendo ...
Exception in thread "Thread-8773" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2786)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94)
at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61)
at com.test.HTTPClient.publishFile(HTTPClient.java:110)
at com.test.HttpFileTransport.put(HttpFileTransport.java:97)
Algunos (incluyéndome a mí) consideran que es grosero usar crosspost: http://forums.sun.com/thread.jspa?threadID=5424210 Especialmente cuando ni siquiera mencionas el hecho. –
Por favor, no te ofendas en la edición donde critiqué tu código. Es mejor que la media, pero tiene margen de mejora. Todo el código no trivial lo hace. Y es fácil estropear el manejo de excepciones: obtuve un merecido -1 hace una semana más o menos cuando simplemente escribí un ejemplo try/catch/finally sin dejar que mi compilador lo comprobara. – kdgregory