七牛云上传文件工具类

 1 方法1  upload(String file, String filename)上传服务器文件路径的demo

2 方法2 

updateFile(MultipartFile file, String filename) 上传springmvc multipartFile 前端上传的文件流上传demo,

仅供参考,有问题多多指教

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class QinniuUtil {

    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static String ACCESS_KEY = "your access_key"; //这两个登录七牛 账号里面可以找到
    private static String SECRET_KEY = "your secret_key";

    //要上传的空间
    private static String bucketname = "bucketname1"; //对应要上传到七牛上 你的那个路径(自己建文件夹 注意设置公开)

    //密钥配置
    private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    //创建上传对象
    private static UploadManager uploadManager = new UploadManager();

    //简单上传,使用默认策略,只需要设置上传的空间名就可以了
    public static String getUpToken(){
        return auth.uploadToken(bucketname);
    }

    /** 普通上传[根据文件路径]
     *
     * @param file
     * @param filename 上传到七牛后保存的文件名
     * @throws IOException
     */
    public static void upload(String file, String filename) throws Exception {
        try {
            //调用put方法上传
            Response res = uploadManager.put(file, filename, getUpToken());

            //打印返回的信息
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }

    /**
     *springmvc MultipartFile 上传文件流
     * @param file 上传文件流
     * @param filename 文件名
     * @return
     * @throws Exception
     */

    public static String updateFile(MultipartFile file, String filename) throws Exception {
        //默认不指定key的情况下,以文件内容的hash值作为文件名
        try {
            InputStream inputStream=file.getInputStream();
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[600]; //buff用于存放循环读取的临时数据
            int rc = 0;
            while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }

            byte[] uploadBytes  = swapStream.toByteArray();
            try {
                Response response = uploadManager.put(uploadBytes,filename,getUpToken());
                //解析上传成功的结果
                DefaultPutRet putRet;
                putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                return putRet.key;

            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                }
            }
        } catch (UnsupportedEncodingException ex) {
        }
        return null;
    }

    public static void main(String args[]) throws Exception {
        new QinniuUtil().upload("d:\\\\20151207175926.jpg", "201801311508134441233.png");
    }

}

 

你可能感兴趣的:(小功能)