发表于: 2017-07-23 21:23:31

1 1103


今天完成的事情:任务七

看其他师兄的日报容联的短信用不了了,金山云的存储也不对个人开放了,学习七牛云第三方API,学习简单的文件上传.

package fileupload;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;

import java.io.*;
import java.net.*;


public class QiniuService implements StorageService {

private Auth auth;
    public QiniuService(){
        cfg new Configuration(Zone.zone1());
        uploadManager new UploadManager(cfg);
    }
    public QiniuService(String accessKeyString secretKeyString bucket) {
        this();
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucket = bucket;
        auth = Auth.create(accessKeysecretKey);
    }
    public boolean upload(byte [] bytesString key){
        String upToken = auth.uploadToken(bucketkey);
        try {
            Response response = uploadManager.put(byteskeyupToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString()DefaultPutRet.class);
            //System.out.println(putRet.key);
            //System.out.println(putRet.hash);
            return  true;
        catch (QiniuException ex) {
            Response r = ex.response;
            logger.error("picture upload failed" + r.toString());
            try {
                System.err.println(r.bodyString());
            catch (QiniuException ex2) {
                //ignore
            }
        }
        return false;
    }
    public byte [] download(String key) throws UnsupportedEncodingExceptionMalformedURLException {
        String domainOfBucket = "http://ot36zr42o.bkt.clouddn.com/";
        String encodedFileName = URLEncoder.encode(key"utf-8");
        String finalUrl = String.format("%s/%s"domainOfBucketencodedFileName);
        URL url = new URL(finalUrl);
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            copy(inStreamoutput);
            byte [] bytes = output.toByteArray();
            return bytes;
        catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

明天计划的事情:问下师兄到底怎么搞,再去看下sendcloud

遇到的问题:都这样了怎么整合啊,jsp页面搞不定,七牛的API只要求文件上传下载么?

收获:星期天效率贼低。


返回列表 返回列表
评论

    分享到