com.givon.baseproject.xinlu.util.UploadMoreImageTask.java Source code

Java tutorial

Introduction

Here is the source code for com.givon.baseproject.xinlu.util.UploadMoreImageTask.java

Source

/* 
 * Copyright 2014 JiaJun.Ltd  All rights reserved.
 * SiChuan JiaJun.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @UploadMoreImageTask.java  2014923 ?4:03:22 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.givon.baseproject.xinlu.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

import com.alibaba.fastjson.JSON;
import com.givon.baseproject.xinlu.cookie.ShareCookie;
import com.givon.baseproject.xinlu.entity.DetailImages;
import com.upyun.api.Uploader;
import com.upyun.api.utils.UpYunException;
import com.upyun.api.utils.UpYunUtils;

public class UploadMoreImageTask extends AsyncTask<Object, Void, ArrayList<DetailImages>> {
    int total;
    private int type;
    ArrayList<String> error = new ArrayList<String>();
    private UpLoadListener upLoadListener;
    private static final String TEST_API_IMAGE_KEY = "lVBfg35QWT+r/LMFsoJjiQ5deno="; // ?api?
    private static final String TEST_API_FILE_KEY = "l5i6un8Z3eacrLB+6MNUX4EG67Y="; // ?api?
    private static final String FILE_BUCKET_NAME = "zuji-app-file"; // 
    private static final String PIC_BUCKET_NAME = "tiangou-app-img"; // 
    private static final long EXPIRATION = System.currentTimeMillis() / 1000 + 600; // ?
    public static final int UPLOAD_FILE = 1;
    public static final int UPLOAD_IMAGE = 2;

    public UpLoadListener getUpLoadListener() {
        return upLoadListener;
    }

    public void setUpLoadListener(UpLoadListener upLoadListener) {
        this.upLoadListener = upLoadListener;
    }

    @Override
    protected ArrayList<DetailImages> doInBackground(Object... params) {
        ArrayList<DetailImages> data = (ArrayList<DetailImages>) params[0];
        ArrayList<DetailImages> strings = new ArrayList<DetailImages>();
        total = data.size();
        type = (Integer) params[1];
        try {
            for (int i = 0; i < total; i++) {
                File file = new File(data.get(i).getImageUrl());
                DetailImages detailImages = data.get(i);
                DetailImages detailImages2 = new DetailImages();
                detailImages2.setHeightSize(detailImages.getHeightSize());
                detailImages2.setImageUrl(detailImages.getImageUrl());
                detailImages2.setSequences(detailImages.getSequences());
                detailImages2.setWidthSize(detailImages.getWidthSize());
                if (file.exists()) {
                    String string = upLoad(detailImages.getImageUrl(), type);
                    if (!StringUtil.isEmpty(string)) {
                        detailImages2.setImageUrl(string);
                        strings.add(detailImages2);
                    } else {
                        error.add(detailImages2.getImageUrl());
                    }
                } else {
                    error.add(detailImages2.getImageUrl());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return strings;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(ArrayList<DetailImages> result) {
        super.onPostExecute(result);
        if (result.size() > 0) {
            if (result.size() == total) {
                if (null != upLoadListener) {
                    upLoadListener.UpLoadResult(result);
                }
            } else {
                if (error.size() > 0) {
                    if (null != upLoadListener) {
                        upLoadListener.UpLoadError(error);
                    }
                }
                if (result.size() > 0) {
                    if (null != upLoadListener) {
                        upLoadListener.UpLoadResult(result);
                    }
                } else {
                    if (null != upLoadListener) {
                        upLoadListener.UpLoadFail();
                    }
                }
            }

        } else {
            if (null != upLoadListener) {
                upLoadListener.UpLoadFail();
            }
        }
    }

    private String upLoad(String filePath, int type) {
        String BUCKET = "";
        switch (type) {
        case UPLOAD_FILE:
            BUCKET = FILE_BUCKET_NAME;
            break;
        case UPLOAD_IMAGE:
            BUCKET = PIC_BUCKET_NAME;
            break;
        default:
            BUCKET = PIC_BUCKET_NAME;
            break;
        }
        String string = null;
        // String filePath = params[0];
        try {
            // test/upload
            // ??????????
            //         String SAVE_KEY = File.separator + "test" + File.separator + "upload" + File.separator
            //               + System.currentTimeMillis() + ".jpg";
            String nameString = FileCache.getFileMD5(new File(filePath));
            String SAVE_KEY = File.separator + "footprint" + File.separator + "upload" + File.separator
                    + nameString;
            //         System.out.println("SAVE_KEY:" + SAVE_KEY);
            //         System.out.println("SAVE_KEY:" + filePath);
            // ?base64??policy
            String policy = UpYunUtils.makePolicy(SAVE_KEY, EXPIRATION, BUCKET);

            // ??api??policy??
            // ?http????
            String signature = UpYunUtils.signature(policy + "&" + TEST_API_IMAGE_KEY);

            // bucket
            string = Uploader.upload(policy, signature, BUCKET, filePath);
        } catch (UpYunException e) {
            e.printStackTrace();
        }

        return string;
    }

    public interface UpLoadListener {
        void UpLoadError(ArrayList<String> notUpPath);

        void UpLoadResult(ArrayList<DetailImages> path);

        void UpLoadFail();
        //      void UpLoadFinish();
    }

}