七牛云- Java 端 使用

项目 中需要把 图片放到 图片服务器上托管, 所以使用了七牛, 注册之后每个月 有免费100 万 次get请求,先说说怎么使用:

 1 、注册, 获取自己的AK,SK

2. 在自己的项目里引入 我用的 maven ()

 

 

 

 

 

 

这里是源码, 源码我们下载没用, 主要是下了jar 包, 然后引入到pom

 

 

 

基本ok 了, 以下 就是使用。

 

 

 

 

 

 

package cn.ycmedia.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

/**
 * @author 朱良兴
 * 七牛 图片服务器 操作展示层
 *
 */
@RestController
public class QiniuImgController {
    
     UploadManager um = new UploadManager();
     public static final String  AK="rzPOFiue_68j1lOwApPw4qjSxf8BkXB1CGJF6R6j";
     public static final String  SK="TMqFfkz-2ASmgpk051gqPOx-sRdwvZHgbZAmwQ0g";
     Auth auth = Auth.create(AK, SK);
    
    /**
     * @param fileName 文件价地址
     * @param dirBundle 文件目录
     * @return
     * @throws Exception 
     */
     @RequestMapping(value = "upload", method = RequestMethod.GET)
    public String uploadImgFile(@RequestParam("fileName")String  fileName,
            @RequestParam("dirBundle")String  dirBundle) throws Exception{
           String token =auth.uploadToken(dirBundle);
           showAllFiles(new File(fileName),token);
           return null;
    }
     

     public void upload(String filePath ,String token) throws IOException{
            try {
              //调用put方法上传
              Response res = um.put(filePath, null,token);
              //打印返回的信息
              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
                  }
              }       
          }
     
       public      void showAllFiles(File dir ,String token) throws Exception{
              File[] fs = dir.listFiles();
              for(int i=0; i<fs.length; i++){
               if(fs[i].isDirectory()){
                try{
                   showAllFiles(fs[i],"");
                  }catch(Exception e){
                  }
               }else{
                   
                   System.err.println(fs[i].getAbsolutePath());
                   //调用put方法上传
                      Response res = um.put(fs[i].getAbsolutePath(), fs[i].getName(),token);
                      //打印返回的信息
                      System.out.println(res.bodyString()); 
                   
               }
              }
            }
     
     
     

}

 

转载于:https://www.cnblogs.com/zgghb/p/5620468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值