칠 우 운: 파일 을 업로드 하고 url 로 되 돌려 줍 니 다.

26031 단어 JAVA
package qiNiu;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

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

import cn.itcast.commons.CommonUtils;

public class QiniuCloudUtil {
	//       AK SK
	private static final String ACCESS_KEY="xxxxx    AK";
	private static final String SECRET_KEY="xxxxx    SK";
	//      bucket
	private static final String BUCKET="          ";
	//    :       
	private static final String DOMAIN="      ";
	/*
	 *      Token
	 */
    public static String getupToken() {
	    //         
		Auth auth = Auth.create(ACCESS_KEY,SECRET_KEY);
		StringMap putPolicy = new StringMap();
		putPolicy.put("returnBody", "{\"fileUrl\": \""+DOMAIN+"$(key)\"}");//        json
		long expireSeconds = 3600;//    
		String upToken = auth.uploadToken(BUCKET, null, expireSeconds, putPolicy);
	    System.out.println(upToken);
	    return upToken;
	}
    
    /**      
     * @param     
     * @return      
     * */
    public static String uploadFile(String localFilePath){
    	//      
    	StringBuffer fileUrl=new StringBuffer(DOMAIN);
    	//        Region       
    	Configuration cfg = new Configuration(Region.region2());
    	//...         

    	UploadManager uploadManager = new UploadManager(cfg);
    	//...      ,      
    	//   Windows   ,    D:\\qiniu\\test.png
    	//String localFilePath = "D:\\IMG_3931.png";
    	//     key    ,      hash      
    	//   key:http://q2yl1ub9s.bkt.clouddn.com/EA3613993A604422A6874095DF94F903
    	String key = CommonUtils.uuid();
    	try {
    	    Response response = uploadManager.put(localFilePath, key, getupToken());
    	    //         
    	    //DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    	    //System.out.println(putRet.key);
    	   // System.out.println(putRet.hash);
    	    fileUrl.append("/"+key);
    	} catch (QiniuException ex) {
    	    Response r = ex.response;
    	    System.err.println(r.toString());
    	    try {
    	        System.err.println(r.bodyString());
    	    } catch (QiniuException ex2) {
    	        //ignore
    	    }
    	}
    	return fileUrl.toString();
    }
    
    public void test1(){
    	System.out.println(uploadFile("D:\\upload.jpg"));
    }
    /**
     *       
     * @param uploadBytes     
     * @param fileName     
     * @return   
     */
    public static String uploadFileBytes(byte[] uploadBytes,String fileName){
    	//      
    	StringBuffer fileUrl=new StringBuffer(DOMAIN);
    	//        Region       
    	Configuration cfg = new Configuration(Region.region2());
    	//...         
    	UploadManager uploadManager = new UploadManager(cfg);
    	//...      ,      
    	//     key    ,      hash      
    	String key = fileName;
    	    try {
    	        Response response = uploadManager.put(uploadBytes, key, getupToken());
    	        fileUrl.append("/"+key);
    	    } catch (QiniuException ex) {
    	        Response r = ex.response;
    	        System.err.println(r.toString());
    	        try {
    	            System.err.println(r.bodyString());
    	        } catch (QiniuException ex2) {
    	            //ignore
    	        }
    	    }

    	    return fileUrl.toString();
    }
    
    public void test2() throws UnsupportedEncodingException{
    	System.out.println(uploadFileBytes("hello qiniu cloud".getBytes("utf-8"),CommonUtils.uuid()));
    }
   /**
    *  //     
    * @param byteInputStream
    * @param fileName
    * @return
    */
    public static String uploadFileStream(InputStream inputStream,String fileName){
    	//      
    	StringBuffer fileUrl=new StringBuffer(DOMAIN);
    	//        Region       
    	Configuration cfg = new Configuration(Region.region2());
    	//...         
    	UploadManager uploadManager = new UploadManager(cfg);
    	//...      ,      
    	//     key    ,      hash      
    	String key = fileName;   	
//    	    byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
    	//    ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);
    	    try {
    	        Response response = uploadManager.put(inputStream,key,getupToken(),null, null);
    	        fileUrl.append("/"+key);
    	    } catch (QiniuException ex) {
    	        Response r = ex.response;
    	        System.err.println(r.toString());
    	        try {
    	            System.err.println(r.bodyString());
    	        } catch (QiniuException ex2) {
    	            //ignore
    	        }
    	    }
    	    return fileUrl.toString();
    	
    }
    @Test
    public void test3() throws UnsupportedEncodingException{
    	System.out.println(
    			uploadFileStream(new ByteArrayInputStream("hello qiniu cloud23232323".getBytes("utf-8")),CommonUtils.uuid())
    			);
    }
}

좋은 웹페이지 즐겨찾기