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字节 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 ishitong.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");//创建一个随机访问文件类,可读写模式 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字节 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"); //接收客户端的开始位置(文件读取到的字节大小) 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 }
posted on 2012-07-24 16:37 Keep Running 阅读(13879) 评论(18) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库