api 使用七牛上传图片

 

1.引入依赖

        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>[7.2.0, 7.2.99]</version>
        </dependency>

2.创建工具类

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Calendar;


public class Base64Util {

    /***
     * * 图片转BASE64
     * @param imagePath 路径
     * @return
     * */
    public static String imageChangeBase64(String imagePath){
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imagePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }



    /**
     * BASE转图片
     * @param baseStr  base64字符串
     * @param imagePath 生成的图片
     * @return
     */
    public static boolean base64ChangeImage(String baseStr,String imagePath){
        if (baseStr == null){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // 解密
            byte[] b = decoder.decodeBuffer(baseStr);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imagePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    public static void DemoQiNiuUploadFile()
    {
        String Path = System.getProperty("user.dir");
        System.out.println("Path:" + Path);

        //加密
        String basestr = imageChangeBase64(Path + "//1.jpeg");
        System.out.println(basestr);
        //解密

        base64ChangeImage(basestr,Path + "//HeadTemp.jpeg");

        long SS = Calendar.getInstance().getTimeInMillis();
        String UploadFile = QiniuyunTools.UploadData(Path + "//HeadTemp.jpeg","123-" + SS);
        System.out.println("UploadFile:" + UploadFile);
    }



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

public class QiniuyunTools {

    public static String UploadData(String FileName,String mobilephone) {

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传
        String accessKey = "RMILAnj3-XHGvN8SSihaDX08AI-vUwFmjhjty6bt"; // RMILAnj3-XHGvN8SSihaDX08AI-vUwFmjhjty6bt
        String secretKey = "MWbZQtik-noIS-c0wJcV1Fvr1ABC-ta-2-G5qmV9"; // MWbZQtik-noIS-c0wJcV1Fvr1ABC-ta-2-G5qmV9
        String bucket = "bucket-o2opda";
        //如果是Windows情况下,格式是 D:\\qiniu\\test.png
        String localFilePath =  FileName ; //"/home/qiniu/test.png";
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = mobilephone + ".jpeg";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            // http://pgu63g1wb.bkt.clouddn.com
            return "http://apppda.easytoline.com/" + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
            return "";
        }
    }

    public static String UploadDataSinger(String FileName,String mobilephone) {

        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证,然后准备上传  从七牛获取
        String accessKey = "*****-XHGvN8SSihaDX08AI-vUwFmjhjty6bt"; 
        String secretKey = "*****-noIS-c0wJcV1Fvr1ABC-ta-2-G5qmV9"; 
        String bucket = "bucket-o2oqianshou";
        //如果是Windows情况下,格式是 D:\\qiniu\\test.png
        String localFilePath =  FileName ; //"/home/qiniu/test.png";
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        String key = mobilephone + ".jpeg";
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
            //http://pgu65veb2.bkt.clouddn.com
            return "http://apppdaqs.easytoline.com/" + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
            return "";
        }
    }
}

 

3.使用base64字符串接口图片 (这是只是用的base64方式而已)

           String singPicUrl="";
            if(Receive.getSignPhoto()!=null && !Receive.getSignPhoto().equals("")) {
                String Path = System.getProperty("user.dir");
                //解密数据
                base64ChangeImage(Receive.getSignPhoto(), Path + "//HandsetSingerTemp.jpeg");
                //时间戳
                long ts = Calendar.getInstance().getTimeInMillis();
                //上传七牛云
                singPicUrl = QiniuyunTools.UploadDataSinger(Path + "//HandsetHeadSinger.jpeg", Receive.getUserID() + Receive.getWaybillList() + ts);
            }

完成

api 使用七牛上传图片

上一篇:高并发 Nginx+Lua OpenResty系列(2)——Nginx Lua API


下一篇:SQL Server 2012实施与管理实战指南(笔记)——Ch5启动SQL Server服务和数据库