android下大文件分割上传

  1. 由于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     }

     

你可能感兴趣的:(android)