欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Volley扩展实现支持进度条的文件上传功能

程序员文章站 2022-04-12 11:43:00
volley是一个轻量级的开源网络通信框架,开源的好处就是可以*定制自己需要的jar包。volley里网络通信时android2.3以上用的httpurlconnecti...

volley是一个轻量级的开源网络通信框架,开源的好处就是可以*定制自己需要的jar包。volley里网络通信时android2.3以上用的httpurlconnection,2.3以下用的httpclient,我做的改动只考虑了2.3以上,不支持2.3版本以下。httpurlconnection默认传输数据是将数据全部写到内存中再发送到服务端,volley就是采用默认的方式,这样在上传大文件时很容易就out of memory,有一种解决办法是设置每次传输流的大小:

已知文件大小:connection .setfixedlengthstreamingmode(long l);

不知道文件大小:connection.setchunkedstreamingmode(1024); //建议使用

android的文件上传一般都是模拟表单,也可以直接socket传,我这里是集成了表单上传,下面是关键类:

public class multipartrequest extends request<string> {
 private final listener<string> mlistener;
 private map<string, string> headermap;
 private map<string, string> mparams;
 private formfile[] files;
 private string boundary = "---------7dc05dba8f3e19";
 
 public multipartrequest(string url, listener<string> listener, map<string, string> params, formfile[] files) {
 this(method.post, url, listener, params, files);
 }
 
 public multipartrequest(int method, string url, listener<string> listener, map<string, string> params, formfile[] files) {
 super(method, url, listener);
 mlistener = listener;
 mparams = params;
 this.files = files;
 }
 
 @override
 public map<string, string> getheaders() throws authfailureerror {
 headermap = new hashmap<string, string>();
 headermap.put("charset", "utf-8");
 //keep-alive
 headermap.put("connection", "keep-alive");
 headermap.put("content-type", "multipart/form-data; boundary=" + boundary);
 return headermap;
 }
 
 @override
 public byte[] getbody() throws authfailureerror {
 //传参数
 stringbuilder sb = new stringbuilder();
 for (map.entry<string, string> entry : mparams.entryset()) {
  // 构建表单字段内容
  sb.append("--");
  sb.append(boundary);
  sb.append("\r\n");
  sb.append("content-disposition: form-data; name=\"" + entry.getkey() + "\"\r\n\r\n");
  sb.append(entry.getvalue());
  sb.append("\r\n");
 }
 return sb.tostring().getbytes();
 }
 
 @override
 public void handrequest(outputstream out) {
 dataoutputstream dos = (dataoutputstream) out;
 try {
  //发送文件数据
  if (files != null) {
  for (formfile file : files) {
   // 发送文件数据
   stringbuilder split = new stringbuilder();
   split.append("--");
   split.append(boundary);
   split.append("\r\n");
   split.append("content-disposition: form-data;name=\"" + file.getparametername() + "\";filename=\"" + file.getfilname() + "\"\r\n");
   split.append("content-type: " + file.getcontenttype() + "\r\n\r\n");
   dos.write(split.tostring().getbytes());
   if (file.getinstream() != null) {
   byte[] buffer = new byte[1024];
   int len = -1;
   int count = 0;
   while ((len = file.getinstream().read(buffer)) != -1) {
    dos.write(buffer, 0, len);
    count += len;
    if (mlistener != null) {
    mlistener.onprogresschange(file.getfilesize(), count);
    }
   }
   count = 0;
   file.getinstream().close();
   } else {
   dos.write(file.getdata(), 0, file.getdata().length);
   }
   dos.write("\r\n".getbytes());
  }
  }
  dos.writebytes("--" + boundary + "--\r\n");
  dos.flush();
 } catch (ioexception e) {
  mlistener.onerror(new volleyerror(e.tostring()));
  try {
  dos.close();
  } catch (ioexception e1) {
  e1.printstacktrace();
  }
 }
 }
 
 @override
 protected response<string> parsenetworkresponse(networkresponse response) {
 string parsed;
 try {
  parsed = new string(response.data, httpheaderparser.parsecharset(response.headers));
 } catch (unsupportedencodingexception e) {
  parsed = new string(response.data);
 }
 return response.success(parsed, httpheaderparser.parsecacheheaders(response));
 }
 
 @override
 protected void deliverresponse(string response) {
 mlistener.onsuccess(response);
 }
 
 @override
 public void delivererror(volleyerror error) {
 mlistener.onerror(error);
 }
}

附上demo连接:android实现文件上传功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。