我想要做 :
文本文件(日志) - > JSON中的字符串 - >上传到服务器 . 我甚至有机会获得11Mo的档案吗?
我的代码:
ArrayList listInfoLog= new ArrayList();
listInfoLog = Tools.readFile(strPathFile); // load my logs from the file as ArrayList
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listInfoLog.size(); i++){
sb.append(listInfoLog.get(i) + "\n");
}
// code en base64
byte[] byteContenu = sb.toString().getBytes("UTF-8"); // OUTOFMEMORY
String byteContenuEncoded64 = Base64.encodeToString(byteContenu, Base64.DEFAULT);
synchroFile.setContenu(byteContenuEncoded64);
JSONObject jsonFile = synchroFile.convertFileToJson();
对getBytes(“UTF8”)的调用会生成一个outofmemory异常 .
12-11 13:49:42.780: E/dalvikvm-heap(13868): Out of memory on a 23040392-byte allocation.
12-11 13:49:42.780: I/dalvikvm(13868): "AsyncTask #3" prio=5 tid=15 RUNNABLE
12-11 13:49:42.780: I/dalvikvm(13868): | group="main" sCount=0 dsCount=0 obj=0x62e4bc60 self=0xab360220
12-11 13:49:42.780: I/dalvikvm(13868): | sysTid=13957 nice=10 sched=0/0 cgrp=apps/bg_non_interactive handle=-1422168792
12-11 13:49:42.780: I/dalvikvm(13868): | state=R schedstat=( 6042297526 1923573007 5457 ) utm=578 stm=26 core=1
12-11 13:49:42.780: I/dalvikvm(13868): at java.nio.charset.Charsets.toUtf8Bytes(Native Method)
12-11 13:49:42.780: I/dalvikvm(13868): at java.lang.String.getBytes(String.java:856)
12-11 13:49:42.780: I/dalvikvm(13868): at java.lang.String.getBytes(String.java:840)
12-11 13:49:42.780: I/dalvikvm(13868): at com.ier.tepv.base.SynchroReceiptAsync.sendLogs(SynchroReceiptAsync.java:951)
12-11 13:49:42.780: I/dalvikvm(13868): at com.ier.tepv.base.SynchroReceiptAsync.doInBackground(SynchroReceiptAsync.java:600)
12-11 13:49:42.780: I/dalvikvm(13868): at com.ier.tepv.base.SynchroReceiptAsync.doInBackground(SynchroReceiptAsync.java:1)
12-11 13:49:42.780: I/dalvikvm(13868): at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-11 13:49:42.780: I/dalvikvm(13868): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-11 13:49:42.780: I/dalvikvm(13868): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-11 13:49:42.780: I/dalvikvm(13868): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-11 13:49:42.780: I/dalvikvm(13868): at java.lang.Thread.run(Thread.java:841)
12-11 13:49:42.780: W/dalvikvm(13868): threadid=15: thread exiting with uncaught exception (group=0x61817bc0)
12-11 13:49:42.780: E/AndroidRuntime(13868): FATAL EXCEPTION: AsyncTask #3
12-11 13:49:42.780: E/AndroidRuntime(13868): Process: com.ier.tepv.base, PID: 13868
12-11 13:49:42.780: E/AndroidRuntime(13868): java.lang.RuntimeException: An error occured while executing doInBackground()
12-11 13:49:42.780: E/AndroidRuntime(13868): at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.lang.Thread.run(Thread.java:841)
12-11 13:49:42.780: E/AndroidRuntime(13868): Caused by: java.lang.OutOfMemoryError
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.nio.charset.Charsets.toUtf8Bytes(Native Method)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.lang.String.getBytes(String.java:856)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.lang.String.getBytes(String.java:840)
12-11 13:49:42.780: E/AndroidRuntime(13868): at com.ier.tepv.base.SynchroReceiptAsync.sendLogs(SynchroReceiptAsync.java:951)
12-11 13:49:42.780: E/AndroidRuntime(13868): at com.ier.tepv.base.SynchroReceiptAsync.doInBackground(SynchroReceiptAsync.java:600)
12-11 13:49:42.780: E/AndroidRuntime(13868): at com.ier.tepv.base.SynchroReceiptAsync.doInBackground(SynchroReceiptAsync.java:1)
12-11 13:49:42.780: E/AndroidRuntime(13868): at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-11 13:49:42.780: E/AndroidRuntime(13868): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-11 13:49:42.780: E/AndroidRuntime(13868): ... 3 more
我不明白我应该如何处理?它工作正常,除了较小的文件(150ko),但在11Mo时崩溃 .
实际上,我从未遇到过这个巨大的日志文件,因为系统开始运行但是为了它,我该怎么办呢?
我试过了 :
将循环分成几部分,但字符串构建器也在外部
用字节流,但我不知道如何在UTF8中设置它 .
FileInputStream fis = new FileInputStream(fileLog); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte [] buf = new byte [1024]; try {for(int readNum;(readNum = fis.read(buf))!= -1;){bos.write(buf,0,readNum); //毫无疑问这里是0}} catch(IOException ex){new TePVException(tagLocal,“sendLog”,“Streaming Log File ex =”ex.getLocalizedMessage()); } byte [] bytes = bos.toByteArray(); byteContenuEncoded64 = Base64.encodeToString(bytes,Base64.DEFAULT);
这是有效的,这是一个很好的一步,但在这里它是outofmemory: String str = json.toString(); 我回到方形一:(