AndroidVolley扩展实现支持进度条的文件上传功能-创新互联
volley是一个轻量级的开源网络通信框架,开源的好处就是可以自由定制自己需要的jar包。volley里网络通信时android2.3以上用的HttpUrlConnection,2.3以下用的HttpClient,我做的改动只考虑了2.3以上,不支持2.3版本以下。HttpUrlConnection默认传输数据是将数据全部写到内存中再发送到服务端,Volley就是采用默认的方式,这样在上传大文件时很容易就out of memory,有一种解决办法是设置每次传输流的大小:
创新互联主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、自适应网站建设、程序开发、网站优化、微网站、成都小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都做网站、成都网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。已知文件大小:connection .setFixedLengthStreamingMode(long l);
不知道文件大小:connection.setChunkedStreamingMode(1024); //建议使用
android的文件上传一般都是模拟表单,也可以直接socket传,我这里是集成了表单上传,下面是关键类:
public class MultipartRequest extends Request{ private final Listener mListener; private Map headerMap; private Map mParams; private FormFile[] files; private String BOUNDARY = "---------7dc05dba8f3e19"; public MultipartRequest(String url, Listener listener, Map params, FormFile[] files) { this(Method.POST, url, listener, params, files); } public MultipartRequest(int method, String url, Listener listener, Map params, FormFile[] files) { super(method, url, listener); mListener = listener; mParams = params; this.files = files; } @Override public Map getHeaders() throws AuthFailureError { headerMap = new HashMap (); 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 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 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); } }
文章名称:AndroidVolley扩展实现支持进度条的文件上传功能-创新互联
本文路径:http://scyanting.com/article/djpiih.html