上传一张或多张图片(使用volley框架)

主要是继承volley的Request类,然后通过使用httpmim的MultipartEntity类对文件参数进行封装,可以上传一个文件或多张文件

public class MultipartRequestUpload extends Request<String> {

    private MultipartEntity entity = new MultipartEntity();
    private VolleyInterface listener;
    private List mFileParts;
    private String mFilePartName;
    private Map mParams;
    /**
      * 单个文件上传
      *
      *
      * @param  url 地址
      * @param  filePartName 文件夹名称
      * @param  file 文件
      * @param  params 参数
      * @param  listener 监听
      *
     */
    public MultipartRequestUpload(String url, String filePartName, File file,
            Map params,VolleyInterface listener) {
        super(Method.POST, url, listener.errorListener());

        mFileParts = new ArrayList();
        if (file != null) {
            mFileParts.add(file);
        }
        this.mFilePartName = filePartName;
        this.listener = listener;
        this.mParams = params;
        buildMultipartEntity();
    }
    /**
      * 多个文件,对应一个key
      *
      * @param  url 地址
      * @param  filePartName 文件夹名称
      * @param  file 文件
      * @param  params 参数
      * @param  listener 监听
      *
      */
    public MultipartRequestUpload(String url, String filePartName, List files,
            Map params,VolleyInterface listener) {
        super(Method.POST, url, listener.errorListener());
        this.mFilePartName = filePartName;
        this.listener = listener;
        this.mFileParts = files;
        this.mParams = params;
        System.out.println(filePartName+"-"+files+"-"+params);
        buildMultipartEntity();
    }
    private void buildMultipartEntity() {
        if (mFileParts != null && mFileParts.size() > 0) {
            for (File file : mFileParts) {
                entity.addPart(mFilePartName, new FileBody(file));
            }
        }

        try {
            if (mParams != null && mParams.size() > 0) {
                for (Map.Entry entry : mParams.entrySet()) {
                    entity.addPart(entry.getKey(),new StringBody(entry.getValue(), Charset.forName("UTF-8")));
                }
            }
        } catch (UnsupportedEncodingException e) {
            VolleyLog.e("UnsupportedEncodingException");
        }
    }

    @Override
    public String getBodyContentType() {
        return entity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            entity.writeTo(bos);
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        }
        return bos.toByteArray();
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        if (VolleyLog.DEBUG) {
            if (response.headers != null) {
                for (Map.Entry entry : response.headers.entrySet()) {
                    VolleyLog.d(entry.getKey() + "=" + entry.getValue());
                }
            }
        }

        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
    public Map getHeaders() throws AuthFailureError {
        VolleyLog.d("getHeaders");
        Map headers = super.getHeaders();

        if (headers == null || headers.equals(Collections.emptyMap())) {
            headers = new HashMap();
        }

        return headers;
    }

    @Override
    protected void deliverResponse(String response) {
        this.listener.loadingListener().onResponse(response);
    }

然后调用这个方法,即可

public static void uploadMultipart(Context context, String url,String fileName,
            List<File> listFile, Map<String, String> params,
            VolleyInterface listener) {
        MultipartRequestUpload request = new MultipartRequestUpload(url,fileName, listFile, params, listener);
        BaseApplication.getHttpQueue().add(request);
        BaseApplication.getHttpQueue().start();
    }

参考:http://my.oschina.net/u/1177694/blog/491834?fromerr=b4AFKFNm

你可能感兴趣的:(android)