android上传大文件亲测可用,上传200M个文件,不到3分钟

之前贴过个例子是android 入门学习笔记 上传大文件 这种的文件大小限制很严,一般30M以上就报错了。网上查了一下,还是推荐用Socket连接进行大文件上传。

今天测试了一下之前网上找的例子,通过Socket实现的android下大文件上传,服务器端用java接收。测试上传了个200M的文件,不到三分钟!还是可以接受的。

只是做了个简单的测试例子,还没有考虑到权限问题(手机上传资料到服务器端,应该需要做身份验证。。)


[html]  view plain copy
  1. connection.setChunkedStreamingMode(chunkSize);    


使用这个代码就可以了,connection为 HttpURLConnection的实例

 

完整代码如下:

 

[html]  view plain copy
  1. /* 上传文件至Server的方法 */  
  2.   private void uploadFile()  
  3.   {  
  4.     String end = "\r\n";  
  5.     String twoHyphens = "--";  
  6.     String boundary = "*****";  
  7.     try  
  8.     {  
  9.       URL url =new URL(actionUrl);  
  10.       HttpURLConnection con=(HttpURLConnection)url.openConnection();  
  11.       con.setChunkedStreamingMode(51200);    
  12.       /* 允许Input、Output,不使用Cache */  
  13.       con.setDoInput(true);  
  14.       con.setDoOutput(true);  
  15.       con.setUseCaches(false);  
  16.       /* 设置传送的method=POST */  
  17.       con.setRequestMethod("POST");  
  18.       /* setRequestProperty */  
  19.       con.setRequestProperty("Connection", "Keep-Alive");  
  20.       con.setRequestProperty("Charset", "UTF-8");  
  21.       con.setRequestProperty("Content-Type",  
  22.                          "multipart/form-data;boundary="+boundary);  
  23.       /* 设置DataOutputStream */  
  24.       DataOutputStream ds =   
  25.         new DataOutputStream(con.getOutputStream());  
  26.       ds.writeBytes(twoHyphens + boundary + end);  
  27.       ds.writeBytes("Content-Disposition: form-data; " +  
  28.                     "name=\"file1\";filename=\"" +  
  29.                     newName +"\"" + end);  
  30.       ds.writeBytes(end);     
  31.   
  32.       /* 取得文件的FileInputStream */  
  33.       FileInputStream fStream = new FileInputStream(uploadFile);  
  34.       /* 设置每次写入1024bytes */  
  35.       int bufferSize = 1024;  
  36.       byte[] buffer = new byte[bufferSize];  
  37.   
  38.       int length = -1;  
  39.       /* 从文件读取数据至缓冲区 */  
  40.       while((length = fStream.read(buffer)) != -1)  
  41.       {  
  42.         /* 将资料写入DataOutputStream中 */  
  43.         ds.write(buffer, 0, length);  
  44.       }  
  45.       ds.writeBytes(end);  
  46.       ds.writeBytes(twoHyphens + boundary + twoHyphens + end);  
  47.   
  48.       /* close streams */  
  49.       fStream.close();  
  50.       ds.flush();  
  51.   
  52.       /* 取得Response内容 */  
  53.       InputStream is = con.getInputStream();  
  54.       int ch;  
  55.       StringBuffer b =new StringBuffer();  
  56.       while( ( ch = is.read() ) != -1 )  
  57.       {  
  58.         b.append( (char)ch );  
  59.       }  
  60.       /* 将Response显示于Dialog */  
  61.       showDialog(b.toString().trim());  
  62.       /* 关闭DataOutputStream */  
  63.       ds.close();  
  64.     }  
  65.     catch(Exception e)  
  66.     {  
  67.       showDialog(""+e);  
  68.     }  
  69.   }  


应要求贴上源码下载地址:http://download.csdn.net/detail/jdsjlzx/8150031

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值