• java上传图片到七牛云


    添加pom.xml依赖

    <dependency>
                <groupId>com.qiniu</groupId>
                <artifactId>qiniu-java-sdk</artifactId>
                <version>[7.2.0, 7.2.99]</version>
    </dependency>    
    QiniuCloudUtilTest.java
    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.util.Auth;
    import org.junit.Test;
    import org.springframework.util.StringUtils;
    
    import java.io.File;
    import java.io.IOException;
    
    public class QiniuCloudUtilTest {
    
        // 设置需要操作的账号的AK和SK
        private static final String ACCESS_KEY = "yourKey";
        private static final String SECRET_KEY = "yourSecret";
    
        // 要上传的空间名
        private static final String bucketname = "youBucketName";
    
        // 密钥
        private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    
        //上传
        public static String upload(File file, String key) throws IOException {
            // 创建上传对象,Zone*代表地区
            //设置华南的服务器
            Configuration configuration = new Configuration(Zone.zone2());
            UploadManager uploadManager = new UploadManager(configuration);
            try {
                // 调用put方法上传
                String token = auth.uploadToken(bucketname);
                if(StringUtils.isEmpty(token)) {
                    System.out.println("未获取到token,请重试!");
                    return null;
                }
                Response res = uploadManager.put(file,key,token);
                // 打印返回的信息
                if (res.isOK()){
                    return key;
                }
            }catch (QiniuException e) {
                Response r = e.response;
                // 请求失败时打印的异常的信息
                e.printStackTrace();
                System.out.println("error "+r.toString());
                try {
                    // 响应的文本信息
                    System.out.println(r.bodyString());
                } catch (QiniuException e1) {
                    System.out.println("error "+e1.error());
                }
            }
            return null;
        }
        @Test
        public void upload() throws IOException {
            File file = new File("D:\工作\20200818\TEST.png");
            String s = upload(file,"O12124K");
            System.out.println(s);
        }
    
      
    }

    上传完在七牛云后台就可以看得见啦胖友们

  • 相关阅读:
    第12章 项目采购管理
    C# 利用xml动态生成带图标菜单
    C#正则表达式整理备忘
    IE8"开发人员工具"使用详解下
    拖盘控件notifyIcon演示例程
    多列选择框控件checkedListBox演示程序
    树形框treeView演示程序
    错误提示控件errorProvider演示例程
    IE8“开发人员工具”使用详解上
    c#中分割字符串的几种方法
  • 原文地址:https://www.cnblogs.com/shisanye/p/13524935.html
Copyright © 2020-2023  润新知