android 大文件分割上传(分块上传)

由于Android自身的原因,对大文件(如视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题。

文件分割后分多次请求服务。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.          }  

文件分割类

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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进行文件整理

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.         
  20.   if(fileType.equals("picture"))  
  21.         {  
  22.             oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw");  
  23.         }  
  24.         else if(fileType.equals("photo"))  
  25.         {  
  26.             oSavedFile = new RandomAccessFile("E:\\"+fileName+".jpg","rw");  
  27.         }  
  28.         else if(fileType.equals("voice"))  
  29.          {  
  30.             oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp3","rw");  
  31.         }  
  32.         else if(fileType.equals("video"))   
  33.         {  
  34.             oSavedFile = new RandomAccessFile("E:\\"+fileName+".mp4", "rw");  
  35.         }  
  36.         //设置标志位,标志文件存储的位置  
  37.         oSavedFile.seek(startPosL);  
  38.         oSavedFile.write(vedioBytes);  
  39.         oSavedFile.close();  
  40.         mResult = "000";  
  41.         return mResult;  
  42.     }  

文件转为string字符串参考:

http://blog.csdn.NET/jdsjlzx/article/details/51194719


用HttpUrlConnection模拟post表单进行文件上传平时很少使用,比较麻烦。

 

原理是: 分析文件上传的数据格式,然后根据格式构造相应的发送给服务器的字符串。

格式如下:这里的httppost123是我自己构造的字符串,可以是其他任何的字符串

----------httppost123 
Content-Disposition: form-data; name="img"; filename="t.txt" 
Content-Type: application/octet-stream 


sdfsdfsdfsdfsdf 
----------httppost123 
Content-Disposition: form-data; name="text" 


text tttt 
----------httppost123-- 

 

上面的表示各个数据必须以结尾。

 

具体Java代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.DataOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.SocketTimeoutException;  
  8. import java.net.URL;  
  9. import java.net.URLEncoder;  
  10. import java.util.HashMap;  
  11. import java.util.Iterator;  
  12. import java.util.Map;  
  13. import java.util.Set;  
  14.   
  15. import javax.imageio.ImageIO;  
  16. import javax.imageio.ImageReader;  
  17. import javax.imageio.stream.ImageInputStream;  
  18.   
  19. public class HttpPostUtil {  
  20.     URL url;  
  21.     HttpURLConnection conn;  
  22.     String boundary = "--------httppost123";  
  23.     Map<String, String> textParams = new HashMap<String, String>();  
  24.     Map<String, File> fileparams = new HashMap<String, File>();  
  25.     DataOutputStream ds;  
  26.   
  27.     public HttpPostUtil(String url) throws Exception {  
  28.         this.url = new URL(url);  
  29.     }  
  30.     //重新设置要请求的服务器地址,即上传文件的地址。  
  31.     public void setUrl(String url) throws Exception {  
  32.         this.url = new URL(url);  
  33.     }  
  34.     //增加一个普通字符串数据到form表单数据中  
  35.     public void addTextParameter(String name, String value) {  
  36.         textParams.put(name, value);  
  37.     }  
  38.     //增加一个文件到form表单数据中  
  39.     public void addFileParameter(String name, File value) {  
  40.         fileparams.put(name, value);  
  41.     }  
  42.     // 清空所有已添加的form表单数据  
  43.     public void clearAllParameters() {  
  44.         textParams.clear();  
  45.         fileparams.clear();  
  46.     }  
  47.     // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组  
  48.     public byte[] send() throws Exception {  
  49.         initConnection();  
  50.         try {  
  51.             conn.connect();  
  52.         } catch (SocketTimeoutException e) {  
  53.             // something  
  54.             throw new RuntimeException();  
  55.         }  
  56.         ds = new DataOutputStream(conn.getOutputStream());  
  57.         writeFileParams();  
  58.         writeStringParams();  
  59.         paramsEnd();  
  60.         InputStream in = conn.getInputStream();  
  61.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  62.         int b;  
  63.         while ((b = in.read()) != -1) {  
  64.             out.write(b);  
  65.         }  
  66.         conn.disconnect();  
  67.         return out.toByteArray();  
  68.     }  
  69.     //文件上传的connection的一些必须设置  
  70.     private void initConnection() throws Exception {  
  71.         conn = (HttpURLConnection) this.url.openConnection();  
  72.         conn.setDoOutput(true);  
  73.         conn.setUseCaches(false);  
  74.         conn.setConnectTimeout(10000); //连接超时为10秒  
  75.         conn.setRequestMethod("POST");  
  76.         conn.setRequestProperty("Content-Type",  
  77.                 "multipart/form-data; boundary=" + boundary);  
  78.     }  
  79.     //普通字符串数据  
  80.     private void writeStringParams() throws Exception {  
  81.         Set<String> keySet = textParams.keySet();  
  82.         for (Iterator<String> it = keySet.iterator(); it.hasNext();) {  
  83.             String name = it.next();  
  84.             String value = textParams.get(name);  
  85.             ds.writeBytes("--" + boundary + "\r\n");  
  86.             ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
  87.                     + "\"\r\n");  
  88.             ds.writeBytes("\r\n");  
  89.             ds.writeBytes(encode(value) + "\r\n");  
  90.         }  
  91.     }  
  92.     //文件数据  
  93.     private void writeFileParams() throws Exception {  
  94.         Set<String> keySet = fileparams.keySet();  
  95.         for (Iterator<String> it = keySet.iterator(); it.hasNext();) {  
  96.             String name = it.next();  
  97.             File value = fileparams.get(name);  
  98.             ds.writeBytes("--" + boundary + "\r\n");  
  99.             ds.writeBytes("Content-Disposition: form-data; name=\"" + name  
  100.                     + "\"; filename=\"" + encode(value.getName()) + "\"\r\n");  
  101.             ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");  
  102.             ds.writeBytes("\r\n");  
  103.             ds.write(getBytes(value));  
  104.             ds.writeBytes("\r\n");  
  105.         }  
  106.     }  
  107.     //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream  
  108.     private String getContentType(File f) throws Exception {  
  109.           
  110. //      return "application/octet-stream";  // 此行不再细分是否为图片,全部作为application/octet-stream 类型  
  111.         ImageInputStream imagein = ImageIO.createImageInputStream(f);  
  112.         if (imagein == null) {  
  113.             return "application/octet-stream";  
  114.         }  
  115.         Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);  
  116.         if (!it.hasNext()) {  
  117.             imagein.close();  
  118.             return "application/octet-stream";  
  119.         }  
  120.         imagein.close();  
  121.         return "image/" + it.next().getFormatName().toLowerCase();//将FormatName返回的值转换成小写,默认为大写  
  122.   
  123.     }  
  124.     //把文件转换成字节数组  
  125.     private byte[] getBytes(File f) throws Exception {  
  126.         FileInputStream in = new FileInputStream(f);  
  127.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  128.         byte[] b = new byte[1024];  
  129.         int n;  
  130.         while ((n = in.read(b)) != -1) {  
  131.             out.write(b, 0, n);  
  132.         }  
  133.         in.close();  
  134.         return out.toByteArray();  
  135.     }  
  136.     //添加结尾数据  
  137.     private void paramsEnd() throws Exception {  
  138.         ds.writeBytes("--" + boundary + "--" + "\r\n");  
  139.         ds.writeBytes("\r\n");  
  140.     }  
  141.     // 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码  
  142.     private String encode(String value) throws Exception{  
  143.         return URLEncoder.encode(value, "UTF-8");  
  144.     }  
  145.     public static void main(String[] args) throws Exception {  
  146.         HttpPostUtil u = new HttpPostUtil("http://localhost:3000/up_load");  
  147.         u.addFileParameter("img"new File(  
  148.                 "D:\\素材\\圆月.jpg"));  
  149.         u.addTextParameter("text""中文");  
  150.         byte[] b = u.send();  
  151.         String result = new String(b);  
  152.         System.out.println(result);  
  153.   
  154.     }  
  155.   
  156. }  

如果不把中文转成UTF-8的格式进行传输,则后台显示中文乱码。

同样,如果其他参数包含中文,则也应当先转码。

当然,具体什么编码要和后台接收的编码一致。


简单的demo:https://github.com/jdsjlzx/uploadFile

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值