1. 程式人生 > >android Volley 上傳檔案上傳圖片

android Volley 上傳檔案上傳圖片

Volley不解釋了吧, android 官方的一個網路請求庫.

原始碼的地址在:

[email protected]:com314159/VolleyMultiPartRequest.git

上面的是ssh

下面的是http地址

https://github.com/com314159/VolleyMultiPartRequest

是根據

https://github.com/smanikandan14/Volley-demo

這位大神修改而來的, 但是那位大神的程式碼有bug, 上傳檔案不成功.

注: 我的demo裡面還集成了okhttp, 不需要的同學不用理這個類即可

實現方法:

1.新增三個jar包,

httpcore-4.3.2.jar

httpclient-4.3.5.jar

httpmime-4.3.5.jar

2.實現MultiPartStack

package com.example.volleytest;


import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.HurlStack;

/**
 * @author ZhiCheng Guo
 * @version 2014年10月7日 上午11:00:52 這個Stack用於上傳檔案, 如果沒有這個Stack, 則上傳檔案不成功
 */
public class MultiPartStack extends HurlStack {
	@SuppressWarnings("unused")
	private static final String TAG = MultiPartStack.class.getSimpleName();
    private final static String HEADER_CONTENT_TYPE = "Content-Type";
	
	
	
	
	@Override
	public HttpResponse performRequest(Request<?> request,
			Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
		
		if(!(request instanceof MultiPartRequest)) {
			return super.performRequest(request, additionalHeaders);
		}
		else {
			return performMultiPartRequest(request, additionalHeaders);
		}
	}
	
    private static void addHeaders(HttpUriRequest httpRequest, Map<String, String> headers) {
        for (String key : headers.keySet()) {
            httpRequest.setHeader(key, headers.get(key));
        }
    }
	
	public HttpResponse performMultiPartRequest(Request<?> request,
			Map<String, String> additionalHeaders)  throws IOException, AuthFailureError {
        HttpUriRequest httpRequest = createMultiPartRequest(request, additionalHeaders);
        addHeaders(httpRequest, additionalHeaders);
        addHeaders(httpRequest, request.getHeaders());
        HttpParams httpParams = httpRequest.getParams();
        int timeoutMs = request.getTimeoutMs();
        // TODO: Reevaluate this connection timeout based on more wide-scale
        // data collection and possibly different for wifi vs. 3G.
        HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
        HttpConnectionParams.setSoTimeout(httpParams, timeoutMs);
        
        
        /* Make a thread safe connection manager for the client */
        HttpClient httpClient = new DefaultHttpClient(httpParams);

        return httpClient.execute(httpRequest);
	}
	
	

    static HttpUriRequest createMultiPartRequest(Request<?> request,
            Map<String, String> additionalHeaders) throws AuthFailureError {
        switch (request.getMethod()) {
            case Method.DEPRECATED_GET_OR_POST: {
                // This is the deprecated way that needs to be handled for backwards compatibility.
                // If the request's post body is null, then the assumption is that the request is
                // GET.  Otherwise, it is assumed that the request is a POST.
                byte[] postBody = request.getBody();
                if (postBody != null) {
                    HttpPost postRequest = new HttpPost(request.getUrl());
                    if(request.getBodyContentType() != null)
                    	postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                    HttpEntity entity;
                    entity = new ByteArrayEntity(postBody);
                    postRequest.setEntity(entity);
                    return postRequest;
                } else {
                    return new HttpGet(request.getUrl());
                }
            }
            case Method.GET:
                return new HttpGet(request.getUrl());
            case Method.DELETE:
                return new HttpDelete(request.getUrl());
            case Method.POST: {
                HttpPost postRequest = new HttpPost(request.getUrl());
                postRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setMultiPartBody(postRequest,request);
                return postRequest;
            }
            case Method.PUT: {
                HttpPut putRequest = new HttpPut(request.getUrl());
                if(request.getBodyContentType() != null)
                	putRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                setMultiPartBody(putRequest,request);
                return putRequest;
            }
            // Added in source code of Volley libray.
            case Method.PATCH: {
            	HttpPatch patchRequest = new HttpPatch(request.getUrl());
            	if(request.getBodyContentType() != null)
            		patchRequest.addHeader(HEADER_CONTENT_TYPE, request.getBodyContentType());
                return patchRequest;
            }
            default:
                throw new IllegalStateException("Unknown request method.");
        }
    }
	
	/**
	 * If Request is MultiPartRequest type, then set MultipartEntity in the
	 * httpRequest object.
	 * 
	 * @param httpRequest
	 * @param request
	 * @throws AuthFailureError
	 */
	private static void setMultiPartBody(HttpEntityEnclosingRequestBase httpRequest,
			Request<?> request) throws AuthFailureError {

		// Return if Request is not MultiPartRequest
		if (!(request instanceof MultiPartRequest)) {
			return;
		}

		// MultipartEntity multipartEntity = new
		// MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

		MultipartEntityBuilder builder = MultipartEntityBuilder.create();

		/* example for setting a HttpMultipartMode */
		builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

		// Iterate the fileUploads
		Map<String, File> fileUpload = ((MultiPartRequest) request).getFileUploads();
		for (Map.Entry<String, File> entry : fileUpload.entrySet()) {

			builder.addPart(((String) entry.getKey()), new FileBody((File) entry.getValue()));
		}

		ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
		// Iterate the stringUploads
		Map<String, String> stringUpload = ((MultiPartRequest) request).getStringUploads();
		for (Map.Entry<String, String> entry : stringUpload.entrySet()) {
			try {
				builder.addPart(((String) entry.getKey()),
						new StringBody((String) entry.getValue(), contentType));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		httpRequest.setEntity(builder.build());
	}

}


3. 實現MultiPartRequest, 這個介面是為了方便擴充套件

package com.example.volleytest;

import java.io.File;
import java.util.Map;

/**
 * @author ZhiCheng Guo
 * @version 2014年10月7日 上午11:04:36
 */
public interface MultiPartRequest {

    public void addFileUpload(String param,File file); 
    
    public void addStringUpload(String param,String content); 
    
    public Map<String,File> getFileUploads();
    
    public Map<String,String> getStringUploads(); 
}
/**
 * Copyright 2013 Mani Selvaraj
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.volleytest;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

/**
 * MultipartRequest - To handle the large file uploads.
 * Extended from JSONRequest. You might want to change to StringRequest based on your response type.
 * @author Mani Selvaraj
 *
 */
public class MultiPartStringRequest extends Request<String> implements MultiPartRequest{

	private final Listener<String> mListener;
	/* To hold the parameter name and the File to upload */
	private Map<String,File> fileUploads = new HashMap<String,File>();
	
	/* To hold the parameter name and the string content to upload */
	private Map<String,String> stringUploads = new HashMap<String,String>();
	
    /**
     * Creates a new request with the given method.
     *
     * @param method the request {@link Method} to use
     * @param url URL to fetch the string at
     * @param listener Listener to receive the String response
     * @param errorListener Error listener, or null to ignore errors
     */
    public MultiPartStringRequest(int method, String url, Listener<String> listener,
            ErrorListener errorListener) {
        super(method, url, errorListener);
        mListener = listener;
    }



    public void addFileUpload(String param,File file) {
    	fileUploads.put(param,file);
    }
    
    public void addStringUpload(String param,String content) {
    	stringUploads.put(param,content);
    }
    
    /**
     * 要上傳的檔案
     */
    public Map<String,File> getFileUploads() {
    	return fileUploads;
    }
    
    /**
     * 要上傳的引數
     */
    public Map<String,String> getStringUploads() {
    	return stringUploads;
    }
    

    @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) {
		if(mListener != null) {
			mListener.onResponse(response);
		}
	}
	
	/**
	 * 空表示不上傳
	 */
    public String getBodyContentType() {
        return null;
    }
}

3.使用方法和volley原生的request的使用方法是一樣的, 只是要使用新的Stack, 如下面的是put的方法的使用方法. 如果是post, 就修改成post方法就可以了

        RequestQueue mSingleQueue = Volley.newRequestQueue(this, new MultiPartStack());


    MultiPartStringRequest multiPartRequest = new MultiPartStringRequest(
            Request.Method.PUT, url, responseListener, errorListener) {

        @Override
        public Map<String, File> getFileUploads() {
            return files;
        }

        @Override
        public Map<String, String> getStringUploads() {
            return params;
        }

    };

    mSingeQueue.add(multiPartRequest)

相關推薦

android Volley 檔案圖片

Volley不解釋了吧, android 官方的一個網路請求庫. 原始碼的地址在: [email protected]:com314159/VolleyMultiPartRequest.git 上面的是ssh 下面的是http地址 https://github.

Android中自定義MultipartEntity實現檔案以及使用Volley庫實現檔案

最近在參加CSDN部落格之星,希望大家給投一票,謝謝啦~                       點這裡投我一票吧~前言在開發當中,我們常常需要實現檔案上傳,比較常見的就是圖片上傳,比如修改個頭像什麼的。但是這個功能在Android和iOS中都沒有預設的實現類,對於And

Ajax檔案之上圖片

這用到iframe+form形式上傳圖片 簡單的  不利用formData物件 onchange=''   也是用來觸發事件,當狀態一改變就執行寫入的函式 為了確保圖片路徑要唯一:uuid+obj.name onload=''  做回撥函式,對後端傳來的資料處理 前端

Android Retrofit2.0實現檔案和下載

這篇主要介紹retrofit框架的上傳檔案到伺服器和從伺服器下載功能的實現 上傳檔案到伺服器 首先 retrofit2.0之前上傳檔案到伺服器需要一個叫TypedFile這個類,但是在2.0版本,這個類被取消了,因為2.0以後,retrofit內部集成了okhttp

C# HttpClient Post 引數同時檔案 圖片 呼叫介面

// 呼叫介面上傳檔案 using (var client = new HttpClient()) { using (var multipartFormDataContent = new MultipartFormDataContent()) { var val

iOS檔案或base64(圖片)之AFNetworking 3.0+檔案圖片

1. base64 上傳圖片 /**  *  上傳圖片到伺服器  *  *  @param image  *  @param photoID  *  @param photoType  */ - (

Android利用HttpURLConnection實現檔案

普通Java應用 瀏覽器在上傳的過程中是將檔案以流的形式提交到伺服器端的,如果直接使用Servlet獲取上傳檔案的輸入流然後再解析裡面的請求引數是比較麻煩,所以Java中Web端可以用的上傳元件有兩種: FileUpload【操作比較複雜】struts上傳的功能就是基於這個實現的。

Android客戶端多檔案

在web開發中,多檔案上傳時是非常方便的,直接使用Http協議提交資料即可。格式如下: <FORM METHOD=POST ACTION="http://192.168.1.101:80

用pscp命令將windows檔案到linux

1.pscp -P 埠號 檔案位置 伺服器:目標位置 提示拒絕設定環境變數。 2.這時開啟putty設定如下: 將紅色圈出的變數刪除。 3.再輸入步驟1中的命令   到putty中查詢是否已上傳 可以看到test.txt已上傳到linux系統下

檔案批量斷點續檔案

接上篇文章 java 超大檔案分片上傳 在其基礎上繼續實現 斷點續傳和檔案秒傳功能 在上篇中,我們可以使用 file. slice 方法對檔案進行分片,可以從後臺讀到當前檔案已經上傳的大小,就可以知道從哪裡開始切片,斷點續傳的原理就是基於這個的。 前端計算檔案的 md5 ,後臺資料庫查詢一遍(前提是把 md5

Android客戶端使用OkGo檔案或者圖片,客戶端和服務端程式碼分享

(一)上傳單個檔案或者圖片: 客戶端程式碼: /** * 儲存資料到伺服器 */ private void saveToInternet() { //上傳單個檔案 String url = Constants.USER_NET_ICON;   Fi

Android H5拍照或選擇圖片+WebApi服務端儲存檔案

H5拍照+H5選照片 <em id="clock_imgs"></em><a href="javascript:captureImage();">選擇圖片</a> <button type="button" id="submit" >

Android WebView-H5互動檔案(包括圖片)

WebView 在4.4前後的區別非常大, 比如對URL跳轉的格式, 對JS的注入宣告等等, 4.4以後的WebView 已經是chromium核心, 有多強大就無需我贅述. 說這些, 其實也是為了說明也因為WebView的前後變化太大了, 所以在低版本和版本上, Web

Android volley(5)MultipartEntity 封裝 volley —— 一個引數多張圖、多張圖片多張圖

一、前言 Google自從2013的IO大會上釋出volley框架之後就受到廣泛應用,的確,用過幾個網路請求庫,感覺volley還是很好用的,用起來也特別方便順手。但是遇到上傳檔案就比較麻煩,尤

Android Webview實現圖片檔案及啟動相機功能

直接上程式碼,體會Webview強大的功能。 webView.setWebChromeClient(webChromeClient);private String mCameraFilePath = null; private ValueCallback<Uri&g

AndroidAndroid與伺服器互動 POST多個圖片檔案、文字內容 GET下載圖片

這裡伺服器端採用的是php解析內容 HTTP請求   HTTP 請求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 這幾種。用於資料互動的最基本方法一般為GET、POST、PUT、DELETE。對

android之使用GridView+仿微信圖片功能(附源代碼)

相冊 ada nbu [] for round pen fromfile idt   由於工作要求最近在使用GridView完成圖片的批量上傳功能,我的例子當中包含仿微信圖片上傳、拍照、本地選擇、相片裁剪等功能,如果有需要的朋友可以看一下,希望我的實際經驗能對您有所幫助。

圖片檔案示例

程式碼片段: //新增post @RequestMapping(method=RequestMethod.POST, value="/addPost") @ResponseBody public int addPost(Post post, MultipartFile file

Java Springboot結合FastDFS實現檔案以及根據圖片url將圖片圖片伺服器

上一篇文章我們已經講解了如何搭建FastDFS圖片伺服器,環境我們準備好了現在就讓我們開始與Java結合將他應用到實際的專案中吧。本篇文章我們將會展示上傳圖片到FastDFS圖片伺服器以及通過外網的圖片url將圖片上傳至我們自己的圖片伺服器中。 1.建立springbo

java圖片-檔案/圖片到七牛

java圖片上傳-檔案(圖片)上傳到七牛 一、在配置檔案中引入依賴(注:七牛開發者url:https://developer.qiniu.com/kodo) <-- 引入依賴 --> <dependency> <