SoFunction
Updated on 2025-04-09

Android Volley extension implements file upload function that supports progress bars

Volley is a lightweight open source network communication framework. The advantage of open source is that you can freely customize the jar packages you need. In volley, when network communication is used in android2.3 or above, and HttpClient is used in Http2.3 or below. The changes I made only consider above 2.3 and do not support version 2.3 or below. HttpUrlConnection defaults to transfer data to write all the data into memory and then send it to the server. Volley adopts the default method, so it is easy to out of memory when uploading large files. There is a solution to set the size of each transmission stream:

Known file size: connection .setFixedLengthStreamingMode(long l);

Don't know the file size: (1024); //It is recommended to use

Android file uploads are usually simulated forms, and can also be directly transmitted to sockets. I have integrated form uploads here, and the following are the key categories:

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(, 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;
  = files;
 }
 
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 headerMap = new HashMap<String, String>();
 ("Charset", "UTF-8");
 //Keep-Alive
 ("Connection", "Keep-Alive");
 ("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
 return headerMap;
 }
 
 @Override
 public byte[] getBody() throws AuthFailureError {
 //Pass parameters StringBuilder sb = new StringBuilder();
 for (<String, String> entry : ()) {
  // Build form field content  ("--");
  (BOUNDARY);
  ("\r\n");
  ("Content-Disposition: form-data; name=\"" + () + "\"\r\n\r\n");
  (());
  ("\r\n");
 }
 return ().getBytes();
 }
 
 @Override
 public void handRequest(OutputStream out) {
 DataOutputStream dos = (DataOutputStream) out;
 try {
  //Send file data  if (files != null) {
  for (FormFile file : files) {
   // Send file data   StringBuilder split = new StringBuilder();
   ("--");
   (BOUNDARY);
   ("\r\n");
   ("Content-Disposition: form-data;name=\"" + () + "\";filename=\"" + () + "\"\r\n");
   ("Content-Type: " + () + "\r\n\r\n");
   (().getBytes());
   if (() != null) {
   byte[] buffer = new byte[1024];
   int len = -1;
   int count = 0;
   while ((len = ().read(buffer)) != -1) {
    (buffer, 0, len);
    count += len;
    if (mListener != null) {
    ((), count);
    }
   }
   count = 0;
   ().close();
   } else {
   ((), 0, ().length);
   }
   ("\r\n".getBytes());
  }
  }
  ("--" + BOUNDARY + "--\r\n");
  ();
 } catch (IOException e) {
  (new VolleyError(()));
  try {
  ();
  } catch (IOException e1) {
  ();
  }
 }
 }
 
 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
 String parsed;
 try {
  parsed = new String(, ());
 } catch (UnsupportedEncodingException e) {
  parsed = new String();
 }
 return (parsed, (response));
 }
 
 @Override
 protected void deliverResponse(String response) {
 (response);
 }
 
 @Override
 public void deliverError(VolleyError error) {
 (error);
 }
}

Attached demo link:Android implements file upload function

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.