七牛雲- 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()); 
                   
               }
              }
            }
     
     
     

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM