Ausgabestrom OutOfMemoryError beim senden von HTTP

Ich versuche zu veröffentlichen, ein großes video - /Bild-Datei aus dem lokalen Dateisystem zu einem http-Pfad, sondern ich Laufe in ein " out of memory Fehler nach einiger Zeit...

hier ist der code

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;

}

Hier ist der Fehler, ich bin immer...

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)
Einige (einschließlich mich) betrachtet ist es unhöflich zu crosspost: forums.sun.com/thread.jspa?threadID=5424210 vor Allem, wenn Sie nicht einmal schweigen von der Tatsache.
Bitte nehmen Sie nicht Anstoß an der edit-wo ich kritisierte deinen code. Es ist besser als der Durchschnitt, hat aber Raum für Verbesserungen. Alle nicht-trivialen code funktioniert. Und es ist einfach zu versauen, exception-handling: ich bekam einen wohlverdienten -1 eine Woche oder so vor, wenn ich gerade eingegeben haben, in ein Beispiel-try/catch/finally, ohne dass mein compiler überprüfen.

InformationsquelleAutor ashchawla | 2010-01-17

Schreibe einen Kommentar