我想上传文件到网络服务器使用代码。但当尺寸> 6M ,抛出致命异常 java.lang.OutOfMemoryError
HttpURLConnection conn;
URL url = new URL(mServerUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", file.getContentType());
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Charset", "UTF-8");
conn.connect();
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
int totalSize = 0;
byte[] buffer = new byte[1024];
int len = 0;
while ((len = file.getInStream().read(buffer, 0, 1024)) != -1) {
totalSize += len;
WDLog.i("totalSize = " + totalSize);
os.write(buffer, 0, len);
}
file.getInStream().close();
os.flush();
os.close();
我该怎么做才能解决这个问题? 感谢
有logcat信息:
12-26 19:35:06.976: D/dalvikvm(8952): GC_FOR_MALLOC freed 36718 objects / 1675552 bytes in 53ms
12-26 19:35:06.976: I/dalvikvm-heap(8952): Forcing collection of SoftReferences for 12580880-byte allocation
12-26 19:35:07.006: D/dalvikvm(8952): GC_FOR_MALLOC freed 646 objects / 47808 bytes in 34ms
12-26 19:35:07.006: E/dalvikvm-heap(8952): Out of memory on a 12580880-byte allocation.
12-26 19:35:07.006: I/dalvikvm(8952): "Thread-29" prio=5 tid=8 RUNNABLE
12-26 19:35:07.006: I/dalvikvm(8952): | group="main" sCount=0 dsCount=0 s=N obj=0x46230978 self=0x30eb98
12-26 19:35:07.006: I/dalvikvm(8952): | sysTid=8980 nice=0 sched=0/0 cgrp=default handle=3159096
12-26 19:35:07.016: I/dalvikvm(8952): | schedstat=( 5075164776 7730011023 9520 )
12-26 19:35:07.016: I/dalvikvm(8952): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:~93)
12-26 19:35:07.016: I/dalvikvm(8952): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
12-26 19:35:07.016: I/dalvikvm(8952): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
12-26 19:35:07.016: I/dalvikvm(8952): at java.io.DataOutputStream.write(DataOutputStream.java:101)
12-26 19:35:07.016: I/dalvikvm(8952): at com.amtcmv.upload.UploadImgVidFile.postVid(UploadImgVidFile.java:218)
12-26 19:35:07.016: I/dalvikvm(8952): at com.amtcmv.upload.UploadImgVidFile.run(UploadImgVidFile.java:95)
12-26 19:35:07.016: I/dalvikvm(8952): at java.lang.Thread.run(Thread.java:1102)
12-26 19:35:07.016: E/dalvikvm(8952): Out of memory: Heap Size=19783KB, Allocated=9352KB, Bitmap Size=162KB
12-26 19:35:07.016: W/dalvikvm(8952): threadid=8: thread exiting with uncaught exception (group=0x400259f8)
12-26 19:35:07.026: E/AndroidRuntime(8952): FATAL EXCEPTION: Thread-29
12-26 19:35:07.026: E/AndroidRuntime(8952): java.lang.OutOfMemoryError
12-26 19:35:07.026: E/AndroidRuntime(8952): at java.io.ByteArrayOutputStream.expand(ByteArrayOutputStream.java:93)
12-26 19:35:07.026: E/AndroidRuntime(8952): at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:218)
12-26 19:35:07.026: E/AndroidRuntime(8952): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$DefaultHttpOutputStream.write(HttpURLConnectionImpl.java:750)
12-26 19:35:07.026: E/AndroidRuntime(8952): at java.io.DataOutputStream.write(DataOutputStream.java:101)
12-26 19:35:07.026: E/AndroidRuntime(8952): at com.amtcmv.upload.UploadImgVidFile.postVid(UploadImgVidFile.java:218)
12-26 19:35:07.026: E/AndroidRuntime(8952): at com.amtcmv.upload.UploadImgVidFile.run(UploadImgVidFile.java:95)
12-26 19:35:07.026: E/AndroidRuntime(8952): at java.lang.Thread.run(Thread.java:1102)
和
os.write(buffer,0,len);
此行抛出错误。
答案 0 :(得分:3)
以 chunk by chunk 为基础将文件上传到服务器。例。首先上传1024字节的数据后,从服务器上传下一个1024字节的数据,依此类推,直到上传文件的所有数据为止。
答案 1 :(得分:1)
如果您在模拟器上测试此代码,请确保您的RAM大小接近实际设备值。