小编ash*_*wla的帖子

发送HTTP时OutputStream OutOfMemoryError

我试图将一个大的视频/图像文件从本地文件系统发布到一个http路径,但一段时间后我遇到内存不足错误...

这是代码

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 = …
Run Code Online (Sandbox Code Playgroud)

java http out-of-memory

14
推荐指数
2
解决办法
1万
查看次数

标签 统计

http ×1

java ×1

out-of-memory ×1