Android利用云盘实现手机上传功能

来源:互联网 发布:shell 脚本编程 编辑:程序博客网 时间:2024/06/06 16:44

http://blog.csdn.net/Tardis1/article/details/50966844,本人地址。
做毕业设计的时候,需要将抓取的tcp、udp报文上传到用户的网盘,不知道是我关键词不好,还是什么的,感觉找不到太多资料。本来想用新浪sae的,但是放弃了,新浪sae功能很强大(就是有点复杂的意思),于是改用了七牛云盘,挺好用的,操作简介,API也是人看的,注册免费,使用超级无敌方便(我不是托。。。)
最后实现了,让用户能把手机上的报文通过wifi上传到自己的云盘,七牛云盘使用很简单,大家注册后,按下面图片就可以看到自己上传的内容:
这里写图片描述
接下来是代码展示:代码粗的那部分是需要使用的时候,按照自己的情况填的,其他部分不需要修改,就可以完成在手机上,将文件上传到指定云盘了,是不是很方便~适合个人小demo,小众APP使用

package com.tools.bishe;import javax.crypto.Mac;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import org.json.JSONObject;import android.content.Context;import android.content.SharedPreferences;import android.os.Environment;import android.preference.Preference;import android.util.Log;import com.MyApplication;import com.qiniu.android.http.ResponseInfo;import com.qiniu.android.storage.UpCompletionHandler;import com.qiniu.android.storage.UploadManager;import com.qiniu.android.utils.UrlSafeBase64;import com.sqlite.bishe.GetAllAppToday;/** *  * @author zsj * 作用:将用户手动抓取的报文,上传到七牛空间中 * @param fileName * */public class PostByQiniu {    static final String TAG = "MainActivity";    String AccessKey = "你的账户";    String SecretKey = "你的密码";    String RoomName="空间名";    private static final String MAC_NAME = "HmacSHA1";    private static final String ENCODING = "UTF-8";    SharedPreferences preferences;    SharedPreferences.Editor editor;    public PostByQiniu(String fileName)    {        //getUserQiniu();//获取用户空间信息        if(fileName!=null)//若传进来的文件名非空,则上传到七牛        {            try {                // 1 构造上传策略                JSONObject _json = new JSONObject();                long _dataline = System.currentTimeMillis() / 1000 + 3600;                _json.put("deadline", _dataline);// 有效时间为一个小时                _json.put("scope", RoomName);                String _encodedPutPolicy = UrlSafeBase64.encodeToString(_json                        .toString().getBytes());                byte[] _sign = HmacSHA1Encrypt(_encodedPutPolicy, **SecretKey**);                String _encodedSign = UrlSafeBase64.encodeToString(_sign);                String _uploadToken = **AccessKey** + ':' + _encodedSign + ':'                        + _encodedPutPolicy;                /**                 * 改成文件存储位置                 */                String SAVE_FILE_DIRECTORY =**fileName**;                //下面是主要方法                UploadManager uploadManager = new UploadManager();                uploadManager.put(SAVE_FILE_DIRECTORY, null, _uploadToken,                        new UpCompletionHandler() {                            public void complete(String key, ResponseInfo info,                                    JSONObject response) {                                Log.e("qiniu", info.toString());                                if(info.statusCode==200)                                {//MyApplication在这里的使用,有必要研究下。告诉UI说我上传成功了                                    MyApplication.message= MyApplication.mhandler.obtainMessage();                                    MyApplication.message.what=3;                                    MyApplication.mhandler.sendMessage(MyApplication.message);                                }                                else                                {//告诉UI说我上传失败了                                    MyApplication.message= MyApplication.mhandler.obtainMessage();                                    MyApplication.message.what=4;                                    MyApplication.mhandler.sendMessage(MyApplication.message);                                }                            }                        }, null);            } catch (Exception e) {                e.printStackTrace();            }        }    }    public static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey)            throws Exception {        byte[] data = encryptKey.getBytes(ENCODING);        // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称        SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);        // 生成一个指定 Mac 算法 的 Mac 对象        Mac mac = Mac.getInstance(MAC_NAME);        // 用给定密钥初始化 Mac 对象        mac.init(secretKey);        byte[] text = encryptText.getBytes(ENCODING);        // 完成 Mac 操作        return mac.doFinal(text);    }    /**     * 若用户有设置个人空间,则钥匙什么的,用用户的     */    public void getUserQiniu()    {        preferences=context.getSharedPreferences("zsj",Context.MODE_WORLD_READABLE);        editor=preferences.edit();//获得指定的sharedpreferences对象        String AccessKey =preferences.getString("AcessKey", null);        String SecretKey = preferences.getString("AcessScrecty", null);        String RoomName=preferences.getString("RoomName", null);        if(AccessKey!=null&&SecretKey!=null&&RoomName!=null)        {            this.AccessKey=AccessKey;            this.SecretKey=SecretKey;            this.RoomName=RoomName;        }    }}上传到云盘的文件名是不规则的,原因是格式没有设置好,这个我查看了是有相关的办法,但是暂时没有时间修改,有知道的朋友可以说下吗,或者我有空再补上吧。
0 0
原创粉丝点击