Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尽管存在缓冲,但从Android应用程序上载大文件时仍会出现内存不足错误_Java_Php_Android_Out Of Memory_Dataoutputstream - Fatal编程技术网

Java 尽管存在缓冲,但从Android应用程序上载大文件时仍会出现内存不足错误

Java 尽管存在缓冲,但从Android应用程序上载大文件时仍会出现内存不足错误,java,php,android,out-of-memory,dataoutputstream,Java,Php,Android,Out Of Memory,Dataoutputstream,我正在尝试使用POST将一个大的视频文件(大约900mb)上传到PHP服务器。我使用FileInputStream读取文件,并使用DataOutputStream将其写入HTTPUrlConnection。尽管有1024字节的缓冲区,但我最终还是没有了内存,下面是我的代码 String result = ""; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { Fil

我正在尝试使用POST将一个大的视频文件(大约900mb)上传到PHP服务器。我使用FileInputStream读取文件,并使用DataOutputStream将其写入HTTPUrlConnection。尽管有1024字节的缓冲区,但我最终还是没有了内存,下面是我的代码

String result = "";

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {

    FileInputStream fileInputStream = new FileInputStream(
            new File(path));

    URL url = new URL(ServerUtils.ip + "upload.php");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type",
            "multipart/form-data;boundary=" + boundary);

    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(conn.getOutputStream()));
    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
            + path + "\"" + lineEnd);
    dos.writeBytes(lineEnd);

    int count;
    byte[]  buffer = new byte[1024];
    while((count = fileInputStream.read(buffer)) > 0){
        dos.write(buffer, 0 , count);
    }

    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    fileInputStream.close();
    dos.flush();
    dos.close();
    BufferedReader rd = new BufferedReader(new InputStreamReader(
            conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        result += line;
    }
    rd.close();

} catch (Exception e) {
    e.printStackTrace();
}
这里是错误

Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 0 free bytes and 3GB until OOM" (recursive case)
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art: "FinalizerDaemon" daemon prio=5 tid=5 Runnable
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:   | group="system" sCount=0 dsCount=0 obj=0x32c071c0 self=0xae085200
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:   | sysTid=4387 nice=0 cgrp=default sched=0/0 handle=0xb42ff930
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:   | state=R schedstat=( 862325936 45498700 428 ) utm=43 stm=43 core=2 HZ=100
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:   | stack=0xb41fd000-0xb41ff000 stackSize=1038KB
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:   | held mutexes= "mutator lock"(shared held)
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:     at com.android.internal.os.BinderInternal$GcWatcher.finalize(BinderInternal.java:53)
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
08-26 14:48:56.276 4379-4387/com.hamzahrmalik.groupwatch W/art:     at java.lang.Thread.run(Thread.java:818)
08-26 14:48:56.277 4379-4387/com.hamzahrmalik.groupwatch E/System: Uncaught exception thrown by finalizer
08-26 14:48:56.277 4379-4387/com.hamzahrmalik.groupwatch I/art: Waiting for a blocking GC Alloc
08-26 14:48:56.277 4379-4722/com.hamzahrmalik.groupwatch I/art: Waiting for a blocking GC Alloc
08-26 14:48:56.277 4379-4414/com.hamzahrmalik.groupwatch I/art: Waiting for a blocking GC Alloc
08-26 14:48:56.313 4379-4385/com.hamzahrmalik.groupwatch I/art: Clamp target GC heap from 208MB to 192MB
08-26 14:48:56.313 4379-4385/com.hamzahrmalik.groupwatch I/art: Alloc concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 192MB/192MB, paused 1.360ms total 38.641ms
08-26 14:48:56.313 4379-4387/com.hamzahrmalik.groupwatch I/art: WaitForGcToComplete blocked for 36.526ms for cause Alloc
08-26 14:48:56.313 4379-4387/com.hamzahrmalik.groupwatch I/art: Starting a blocking GC Alloc
08-26 14:48:56.313 4379-4722/com.hamzahrmalik.groupwatch I/art: WaitForGcToComplete blocked for 108.918ms for cause Alloc
08-26 14:48:56.313 4379-4722/com.hamzahrmalik.groupwatch I/art: Starting a blocking GC Alloc
08-26 14:48:56.313 4379-4414/com.hamzahrmalik.groupwatch I/art: Waiting for a blocking GC Alloc
08-26 14:48:56.314 4379-4387/com.hamzahrmalik.groupwatch I/art: Waiting for a blocking GC Alloc
08-26 14:48:56.314 4379-4385/com.hamzahrmalik.groupwatch W/art: Throwing OutOfMemoryError "Failed to allocate a 28 byte allocation with 0 free bytes and 3GB until OOM" (recursive case)
08-26 14:48:56.338 4379-4385/com.hamzahrmalik.groupwatch W/art: "JDWP" daemon prio=5 tid=3 Runnable
08-26 14:48:56.339 4379-4385/com.hamzahrmalik.groupwatch W/art:   | group="system" sCount=0 dsCount=0 obj=0x32c07100 self=0xae083900
08-26 14:48:56.339 4379-4385/com.hamzahrmalik.groupwatch W/art:   | sysTid=4385 nice=0 cgrp=default sched=0/0 handle=0xb455a930
08-26 14:48:56.339 4379-4385/com.hamzahrmalik.groupwatch W/art:   | state=R schedstat=( 298147394 24472603 183 ) utm=25 stm=4 core=3 HZ=100
08-26 14:48:56.339 4379-4385/com.hamzahrmalik.groupwatch W/art:   | stack=0xb445e000-0xb4460000 stackSize=1014KB
08-26 14:48:56.339 4379-4385/com.hamzahrmalik.groupwatch W/art:   | held mutexes= "mutator lock"(shared held)

问题很可能是由于系统在将请求发送到服务器之前缓冲了请求而导致的。要防止出现这种情况(请求表单被缓冲),可以使用:

conn.setChunkedStreamingMode(....);
其中,
conn
HttpURLConnection
的一个实例


问题可能是您从服务器得到的响应,您将其存储在
字符串中
。您可以尝试使用
conn.setChunkedStreamingMode(..)
选项来防止系统在发送请求之前缓冲请求。@Titus在任何响应到来之前它会崩溃。此外,反应仅仅是“成功”。我会尝试感谢的。我知道,在这种情况下,问题可能是在发送请求之前,请求在系统上被缓冲了。您可以通过使用
setChunkedStreamingMode(…)
来防止这种情况。是的,我现在正在尝试,我会告诉您它是否有效