七牛云存储使用的utils

Posted SDingBa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了七牛云存储使用的utils相关的知识,希望对你有一定的参考价值。

自己弄了一个简单为七牛云的utils文件,基础版本,适合初学者:

上传文件:
Response res = uploadManager.put(file, key, getUpToken());

**put 的第一个参数可以是文件file类型,可以是路径,还可以是byte[]
在web项目中,显然 路径是不行的,可以使用file或者byte[];**


public class qiniuYunUtils 

    private static final Logger LOGGER = LoggerFactory.getLogger(qiniuYunUtils.class);

    private static String buckeName = "sdingba";

    private static String url = "http://************.com/";

    public static String getUrl() 
        return url;
    

    private static Auth authInstance() 
        String access = "*************";
        String secret = "*************";
        // 密钥配置
        Auth auth = Auth.create(access, secret);
        return auth;
    

    /**
     * 普通上传
     * 
     * @return
     */
    private static String getUpToken() 
        return authInstance().uploadToken(buckeName);
    

    /**
     * 覆盖上传
     * 
     * @param key
     * @return
     */
    private static String getUpTokenUpdate(String key) 
        return authInstance().uploadToken(buckeName, key);
    

    /**
     * 更新上传文件
     * 路径上传不适合web项目,所有注释掉了,想要的可以参考上一篇博客自己写一个
     * 
     * @param filePath
     * @param fileName
     */
    // public static void uploadUpdate(String filePath, String fileName) 
    // filePath = "/home/su/图片/桌面/1353977500NrBAhr.jpg";
    // fileName = "sdingbaFileName";
    // File file = new File(filePath);
    // uploadUpdate(file, fileName);
    // 

    /**
     * 更新上传文件
     *
     * @param file <byte[]>
     * @param fileName
     */
    public static void uploadUpdate(byte[] file, String fileName, String orderBy) 
        String key = orderBy + "=" + UUID.randomUUID().toString() + "_" + fileName;
        UploadManager uploadManager = new UploadManager();// 创建上传对象
        try 
            Response res = uploadManager.put(file, key, getUpTokenUpdate(key));
         catch (QiniuException e) 
            Response r = e.response;
            LOGGER.error("更新上传七牛云异常:", r.toString());
            try 
                LOGGER.error("更新上传七牛云异常:", r.bodyString());
                System.out.println();
             catch (QiniuException e1) 
            
        
    

    /**
     * 上传文件
     * 路径上传不适合web项目,所有注释掉了,想要的可以参考上一篇博客自己写一个
     * @param filePath
     * @param fileName
     */
    // public static String upload(String filePath, String fileName) 
    // filePath = "/home/su/图片/桌面/1353977500NrBAhr.jpg";
    // fileName = "sdingbaFileName";
    // File file = new File(filePath);
     return upload(file, fileName);
    // return null;
    // 

    /**
     * 上传文件
     * 
     * @param file byte[]
     * @param fileName
     * @return 文件名
     */
    public static String upload(byte[] file, String fileName, String orderBy) 
        String key = orderBy + "=" + UUID.randomUUID().toString() + "_" + fileName;
        UploadManager uploadManager = new UploadManager();// 创建上传对象
        try 
            Response res = uploadManager.put(file, key, getUpToken());
            return res.bodyString();
         catch (QiniuException e) 
            Response r = e.response;
            LOGGER.error("上传七牛云异常:", r.toString());
            try 
                LOGGER.error("上传七牛云异常:", r.bodyString());
                System.out.println();
             catch (QiniuException e1) 
            
        
        return null;
    

    /**
     * 删除一个文件
     * 
     * @param key
     */
    public static void deleteImg(String key) 
        // 实例化一个 bucketManage
        BucketManager bucketManager = new BucketManager(authInstance());
        try 
            bucketManager.delete(buckeName, key);
         catch (QiniuException e) 
            Response r = e.response;
            System.out.println(r.toString());
        
    

    //批处理删除文件,大家可以自己写一个批处理更新,或者添加的,原理差不多
    public static boolean BatchDeleteImg(List<String> imgPaths) 
        BucketManager bucketManager = new BucketManager(authInstance());
        BucketManager.Batch operations = new BucketManager.Batch();
        for (String url : imgPaths) 
            operations.delete(buckeName, url);
        
        try 
            bucketManager.batch(operations);
            return true;
         catch (QiniuException e) 
            Response r = e.response;
            LOGGER.error("文件删除错误", r.toString());
            e.printStackTrace();
            return false;
        
    

    /**
     * 处理文件格式大小
     *
     * @param srcImageIo InputStream 文件
     * @param width 宽
     * @param height 高
     * @return
     */
    public static byte[] resizeImageFile(InputStream srcImageIo, int width, int height) 
        Image srcImg;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try 
            srcImg = ImageIO.read(srcImageIo);
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
            byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(buffImg, "JPEG", byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
         catch (IOException e) 
            e.printStackTrace();
         finally 
            srcImg = null;
            if (byteArrayOutputStream != null) 
                try 
                    byteArrayOutputStream.close();
                 catch (IOException e) 
                    e.printStackTrace();
                

            
        
        return null;
    

    /**
     * 处理文件格式大小
     * 
     * @param srcImagePath url<本地>
     * @param width
     * @param height
     * @return
     */
    public static byte[] resizeImageFile(String srcImagePath, int width, int height) 
        File file = new File(srcImagePath);
        try 
            InputStream input = new FileInputStream(file);
            return resizeImageFile(input, width, height);
         catch (FileNotFoundException e) 
            e.printStackTrace();
         finally 
        
        return null;
    



/**
     * 判断文件是否是图片格式
     *
     * @param imageFile
     * @return
     */
    private static boolean isImage(InputStream imageFile) 
        Image img = null;
        try 
            img = ImageIO.read(imageFile);
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) 
                return false;
            
            return true;
         catch (Exception e) 
            return false;
         finally 
            img = null;
        
    

---以下2个方法  是  适合我自己用的,处理文件的名称格式
 public String getImageOriginalFileName(String url) 
        int indexFirst = url.indexOf("_") + 1;
        if (indexFirst > 0) 
            return url.substring(indexFirst);
        
        return "";
    

    private String getImageFileOrderBy(String url) 
        int indexFirst = url.indexOf("=");
        if (indexFirst > 0) 
            return url.substring(0, indexFirst);
        
        return "0";
    

以上是关于七牛云存储使用的utils的主要内容,如果未能解决你的问题,请参考以下文章

我为啥最终选择了七牛云存储

七牛云存储 视频获取缩略图

专访吕桂华:七牛云存储未来一定是七牛云!

如何基于七牛云存储+Markdown编辑器做静态博客

2018最新七牛云存储8折充值优惠码

dedecms织梦七牛云存储二次开发