您好,登錄后才能下訂單哦!
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實現文件上傳功能
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。