android培训之android下大文件分割上传(摘自达内总部)
????? 由于android自身的原因,對大文件(如影視頻文件)的*作很容易造成OOM,即:Dalvik堆內存溢出,利用文件分割將大文件分割為小文件可以解決問題。
文件分割后分多次請求服務。
?1 //文件分割上傳
?2???? public? void cutFileUpload(String fileType,String filePath)
?3???? {
?4???????? try
?5???????? {
?6???????????? FileAccessI fileAccessI = new FileAccessI(filePath, 0);
?7???????????? Long nStartPos = 0l;
?8???????????? Long length = fileAccessI.getFileLength();
?9???????????? int mBufferSize = 1024 * 100; //每次處理1024 * 100字節(jié)
10???????????? byte[] buffer = new byte[mBufferSize];
11???????????? FileAccessI.Detail detail;
12???????????? long nRead = 0l;
13???????????? String vedioFileName = Usual.f_getUUID(); //分配一個文件名
14???????????? long nStart = nStartPos;
15???????????? int i = 0;
16???????????? while (nStart < length)
17???????????? {
18???????????????? detail = fileAccessI.getContent(nStart);
19???????????????? nRead = detail.length;
20???????????????? buffer = detail.b;
21???????????????? JSONObject mInDataJson = new JSONObject();
22???????????????? mInDataJson.put("a", "282");
23???????????????? mInDataJson.put("FileName", vedioFileName);
24???????????????? mInDataJson.put("start", nStart); //服務端獲取開始文章進行寫文件
25???????????????? mInDataJson.put("filetype", fileType);
26???????????????? nStart += nRead;
27???????????????? nStartPos = nStart;
28???????????????? String url = UsualA.f_getXmlSOAUrl(UsualA.mServiceFastByteUrl, "n.uploadvedio", mInDataJson.toString(),
29???????????????????????? "282");
30???????????????? HttpFastUtil.f_httpPostByte(url, buffer, false);
31???????????? }
32???????? }
33???????? catch (Exception e)
34???????? {
35???????? }
?
文件分割類
?1 package i****ong.mppsp.android.util;
?2?
?3 import java.io.*;
?4?
?5 public class FileAccessI implements Serializable
?6 {
?7?
?8???? RandomAccessFile oSavedFile;
?9???? long nPos;
10?
11?
12???? public FileAccessI() throws IOException
13???? {
14???????? this("", 0);
15???? }
16???? public FileAccessI(String sName, long nPos) throws IOException
17???? {
18???????? oSavedFile = new RandomAccessFile(sName, "rw");//創(chuàng)建一個隨機訪問文件類,可讀寫模式
19???????? this.nPos = nPos;
20???????? oSavedFile.seek(nPos);
21???? }
22???? public synchronized int write(byte[] b, int nStart, int nLen)
23???? {
24???????? int n = -1;
25???????? try
26???????? {
27???????????? oSavedFile.write(b, nStart, nLen);
28???????????? n = nLen;
29???????? }
30???????? catch (IOException e)
31???????? {
32???????????? e.printStackTrace();
33???????? }
34???????? return n;
35???? }
36???? //每次讀取102400字節(jié)
37???? public synchronized Detail getContent(long nStart)
38???? {
39???????? Detail detail = new Detail();
40???????? detail.b = new byte[102400];
41???????? try
42???????? {
43???????????? oSavedFile.seek(nStart);
44???????????? detail.length = oSavedFile.read(detail.b);
45???????? }
46???????? catch (IOException e)
47???????? {
48???????????? e.printStackTrace();
49???????? }
50???????? return detail;
51???? }
52?
53?
54???? public class Detail
55???? {
56?
57???????? public byte[] b;
58???????? public int length;
59???? }
60?
61???? //獲取文件長度
62???? public long getFileLength()
63???? {
64???????? Long length = 0l;
65???????? try
66???????? {
67???????????? length = oSavedFile.length();
68???????? }
69???????? catch (IOException e)
70???????? {
71???????????? // TODO Auto-generated catch block
72???????????? e.printStackTrace();
73???????? }
74???????? return length;
75???? }
76 }
服務端獲得分割的文件,利用RandomAccessFile進行文件整理
?1 /**
?2????? * 音視頻圖片處理
?3????? * @param mStr
?4????? * @return
?5????? * @throws Exception
?6????? */
?7???? public static String f_uploadVedio(String mStr) throws Exception
?8???? {
?9???????? String mResult = Usual.mEmpty;
10???????? String fileType = Usual.mEmpty;
11???????? int startPosL = 0;
12???????? RandomAccessFile oSavedFile = null;
13???????? JSONObject jsonObject = new JSONObject(mStr);
14???????? String vedioJsonStr = jsonObject.getString("VEDIO");
15???????? byte[] vedioBytes = Usual.f_fromBase64String(vedioJsonStr);
16???????? startPosL = (Integer) jsonObject.get("start"); //接收客戶端的開始位置(文件讀取到的字節(jié)大小)
17???????? fileType = (String)jsonObject.getString("filetype");
18???????? String fileName = (String)jsonObject.getString("FileName");
19???????? if(fileType.equals("picture"))
20???????? {
21???????????? oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw");
22???????? }
23???????? else if(fileType.equals("photo"))
24???????? {
25???????????? oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw");
26???????? }
27???????? else if(fileType.equals("voice"))
28????????? {
29???????????? oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp3","rw");
30???????? }
31???????? else if(fileType.equals("video"))?
32???????? {
33???????????? oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp4", "rw");
34???????? }
35???????? //設置標志位,標志文件存儲的位置
36???????? oSavedFile.seek(startPosL);
37???????? oSavedFile.write(vedioBytes);
38???????? oSavedFile.close();
39???????? mResult = "000";
40???????? return mResult;
41???? }
總結
以上是生活随笔為你收集整理的android培训之android下大文件分割上传(摘自达内总部)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Nifi在Windows环
- 下一篇: SpringBoot异步调用方法