Seven Cows---all kinds of demo__java about the JAVA SDK

Source: Internet
Author: User
Tags auth base64

can refer to the Seven Cow Java SDK Source code as well as the official website to use the document:
Https://github.com/qiniu/java-sdk
Http://developer.qiniu.com/docs/v6/sdk/java-sdk.html

Simple Upload

Import Com.qiniu.util.Auth;
Import java.io.IOException;
Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;

Import Com.qiniu.storage.UploadManager;
  public class Uploaddemo {//Set account Access_key and Secret_key String access_key = "Access_key";
  String Secret_key = "Secret_key";
  To upload the space String bucketname = "Bucket_name";
  After uploading to seven cows, save the filename String key = "My-java.png";

  Upload file path String FilePath = "/.../...";
  Key configuration Auth Auth = Auth.create (Access_key, Secret_key);

  Create an uploaded object UploadManager uploadmanager = new UploadManager ();
  Simple upload, using the default policy, only need to set the space name of the upload can be public String Getuptoken () {return auth.uploadtoken (bucketname);  public void Upload () throws ioexception{try {//Call put method upload Response res = uploadmanager.put (FilePath,
      NULL, Getuptoken ()); 
      Prints the returned information System.out.println (res.bodystring ());
          catch (Qiniuexception e) {Response r = e.response; Information about the exception that was printed when the request failed System.out.printlN (r.tostring ());
          try {//Response text Information System.out.println (r.bodystring ()); The catch (qiniuexception E1) {//ignore}} is public static void main (String args
  []) throws ioexception{new Uploaddemo (). upload ();
 }

}

Overwrite upload

Import java.io.IOException;
Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;
Import Com.qiniu.storage.UploadManager;
Import Com.qiniu.util.Auth;

Import Com.qiniu.util.StringMap;
  public class Uploaddemo {//Set account Access_key and Secret_key String access_key = "Access_key";
  String Secret_key = "Secret_key";
  To upload the space String bucketname = "Bucket_name";
  After uploading to seven cows, save the filename String key = "My-java.png";

  Upload file path String filePath = "/.../...";
  Key configuration Auth Auth = Auth.create (Access_key, Secret_key);

  Create an uploaded object UploadManager uploadmanager = new UploadManager (); Overwrites the upload public String getuptoken () {//<bucket>:<key>, which indicates that only the user is allowed to upload a file with the specified key.
    In this format, the file defaults to "Modify", and a resource with the same name will be overwritten this time.
    If you want to upload only the files for the specified key and do not allow modifications, you can set the following Insertonly property value to 1.
  The third parameter is the expiration time of token return Auth.uploadtoken (Bucketname, key, 3600); public void Upload () throws ioexception{try {//Call put method upload, the key specified here and the upload policy must be consistent Response res = Uploa Dmanager.put (filePath, Key, Getuptoken ()); 
      Prints the returned information System.out.println (res.bodystring ());
          catch (Qiniuexception e) {Response r = e.response;
          Information about the exception that was printed when the request failed System.out.println (r.tostring ());
          try {//Response text Information System.out.println (r.bodystring ()); The catch (qiniuexception E1) {//ignore}} is public static void main (String args
  []) throws ioexception{new Uploaddemo (). upload ();
 }

}

Breakpoint Upload

Import java.io.IOException;
Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;
Import Com.qiniu.storage.Recorder;
Import Com.qiniu.storage.UploadManager;
Import Com.qiniu.storage.persistent.FileRecorder;

Import Com.qiniu.util.Auth;
  public class Uploaddemo {//Set account Access_key and Secret_key String access_key = "Access_key";
  String Secret_key = "Secret_key";
  To upload the space String bucketname = "Bucket_name";
  After uploading to seven cows, save the filename String key = "My-java.png";

  Upload file path String filePath = "/.../...";
  Key configuration Auth Auth = Auth.create (Access_key, Secret_key);

  Create an uploaded object UploadManager uploadmanager = new UploadManager ();
  Overwrite upload public String getuptoken () {return auth.uploadtoken (bucketname);
    The public void upload () throws ioexception{//Set breakpoint record file is saved in the specified folder or the file object String recordpath = "/.../...";
     Instantiate Recorder Object Recorder recorder = new Filerecorder (Recordpath); Instantiate the upload object and pass in a Recorder object UploadManager UploadManager = new UPLOADMANager (recorder);
      try {//Call put method upload Response res = uploadmanager.put ("Path/file", "Key", Getuptoken ()); 
      Prints the returned information System.out.println (res.bodystring ());
          catch (Qiniuexception e) {Response r = e.response;
          Information about the exception that was printed when the request failed System.out.println (r.tostring ());
          try {//Response text Information System.out.println (r.bodystring ()); The catch (qiniuexception E1) {//ignore}} is public static void main (String args
  []) throws ioexception{new Uploaddemo (). upload ();

 }

}

Base64 Upload

Package demo;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpPost;
Import org.apache.http.entity.StringEntity;
Import Org.apache.http.impl.client.HttpClientBuilder;

Import Org.apache.http.util.EntityUtils;
Import Com.qiniu.util.Auth;

Import com.qiniu.util.Base64;
 public class Base64picturedemo {public static void main (string[] args) throws Exception {putb64 ();
  The public static void Putb64 () throws Exception {String Access_key = "Access_key";
  String Secret_key = "Secret_key";

     Auth Auth = auth.create (Access_key, Secret_key);

     String token = Auth.uploadtoken ("Bucket", "key", 3600, NULL);
     FileInputStream FIS = null;

     String file= "Path/file";
      try{//File size int l = (int) (new file (file). Length ());
      String url = "http://up.qiniu.com/putb64/" + L;

      byte[] src = new byte[l];  
   FILE-Input stream-byte array-base64 string      FIS = new FileInputStream (new file);
         Fis.read (SRC);

         String file64 = base64.encodetostring (src, base64.default);
         Constructs a Post object HttpPost post = new HttpPost (URL);
         Post.addheader ("Content-type", "Application/octet-stream");
         Post.addheader ("Authorization", "Uptoken" + token);

         Post.setentity (New Stringentity (file64));
         Request and response httpclient C = httpclientbuilder.create (). build ();

         HttpResponse res = C.execute (POST);
         Output System.out.println (Res.getstatusline ());
         String responsebody = entityutils.tostring (Res.getentity (), "UTF-8");

     System.out.println (responsebody);
         }catch (Exception e) {e.printstacktrace ();
     Throw e;
         }finally{if (FIS!= null) {fis.close (); }
     }
 }

}

Upload & callback

Import Com.qiniu.util.Auth;

Import Com.qiniu.util.StringMap;

Import java.io.IOException;
Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;

Import Com.qiniu.storage.UploadManager;
  public class Uploaddemo {//Set account Access_key and Secret_key String access_key = "Access_key";
  String Secret_key = "Secret_key";
  To upload the space String bucketname = "Bucket_name";
  After uploading to seven cows, save the filename String key = "My-java.png";

  Upload file path String FilePath = "/.../...";
  Key configuration Auth Auth = Auth.create (Access_key, Secret_key);

  Create an uploaded object UploadManager uploadmanager = new UploadManager (); Set Callbackurl and Callbackbody, seven cows callback file name and file size to Business Server public String Getuptoken () {return Auth.uploadtoken (bucketname,n Ull,3600,new Stringmap (). Put ("Callbackurl", "Http://your.domain.com/callback"). Put ("Callbackbody", "F
  ilename=$ (fname) &filesize=$ (fsize))); public void Upload () throws ioexception{try {//Call put method upload Response res = UPLOADMANAGER.PUT (FilePath, NULL, Getuptoken ()); 
      Prints the returned information System.out.println (res.bodystring ());
          catch (Qiniuexception e) {Response r = e.response;
          Information about the exception that was printed when the request failed System.out.println (r.tostring ());
          try {//Response text Information System.out.println (r.bodystring ()); The catch (qiniuexception E1) {//ignore}} is public static void main (String args
  []) throws ioexception{new Uploaddemo (). upload ();
 }

}

Upload & Pre-transfer (for example, video transcoding)

Import Com.qiniu.util.Auth;
Import Com.qiniu.util.StringMap;

Import com.qiniu.util.UrlSafeBase64;

Import java.io.IOException;
Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;

Import Com.qiniu.storage.UploadManager;
  public class Uploaddemo {//Set account Access_key and Secret_key String access_key = "Access_key";
  String Secret_key = "Secret_key";
  To upload the space String bucketname = "Bucket_name";
  After uploading to seven cows, save the filename String key = "My-java.png";

  Upload file path String FilePath = "/.../...";
  Sets the transcoding operation parameter String fops = "avthumb/mp4/s/640x360/vb/1.25m";

  Set the queue String pipeline = "Yourpipelinename" of the transcoding;
  You can customize the file after the transcoding using the SaveAs parameter, and of course you can not specify that the file will be named by default and saved in the current space.
  String urlbase64 = urlsafebase64.encodetostring ("Target bucket_name: Custom file Key");

  String pfops = fops + "|saveas/" + urlbase64;
  Key configuration Auth Auth = Auth.create (Access_key, Secret_key);

  Create an uploaded object UploadManager uploadmanager = new UploadManager (); Set the Persistentops field and persistentpipeline field public String in the upload policy getuPToken () {return Auth.uploadtoken (bucketname,null,3600,new stringmap (). Putnotempty ("Persistentops", Pfops
  ). Putnotempty ("Persistentpipeline", Pipeline), true);  public void Upload () throws ioexception{try {//Call put method upload Response res = uploadmanager.put (FilePath,
      NULL, Getuptoken ()); 
      Prints the returned information System.out.println (res.bodystring ());
          catch (Qiniuexception e) {Response r = e.response;
          Information about the exception that was printed when the request failed System.out.println (r.tostring ());
          try {//Response text Information System.out.println (r.bodystring ()); The catch (qiniuexception E1) {//ignore}} is public static void main (String args
  []) throws ioexception{new Uploaddemo (). upload (); Note: The above demo is only for video transcoding, if you need other functions such as audio and video slices, video screenshots, video stitching only need to modify the FoPs of the following parameters on the above can be, Eg:fops = "vframe/jpg/offset/1/w/480/h/360/
Rotate/90 "means the video screenshot. Here are some common data processing features that you can choose from://------------------Picture Scaling-------------------FoPs = "imageview/2/w/200/h/200";

------------------video transcoding-------------------//FoPs = "AVTHUMB/FLV/VB/229K/VCODEC/LIBX264/NODOMAIN/1"; ------------------picture watermark-------------------String pictureurl = urlsafebase64.encodetostring ("http://
Developer.qiniu.com/resource/logo-2.jpg ");

FoPs = "watermark/1/image/" + pictureurl;
------------------Video Slice-------------------fops = "avthumb/m3u8"; Slice and encryption parameter FoPs = "avthumb/m3u8/vb/640k/hlskey/mdeymzq1njc4oteymzq1ng==/hlskeyurl/

ahr0cdovlzd4bgvryi5jb20ylnowlmdsyi5xaw5pdwnkbi5jb20vcwluaxv0zxn0lmtleq== ";

------------------Document Conversion-------------------FoPs = "Yifangyun_preview";

------------------Video screenshot-------------------fops = "vframe/jpg/offset/1/w/480/h/360/rotate/90"; ------------------Video Stitching-------------------//stitching video clips to ensure that all the source of the screen width is consistent//drop as a data processing object of the source file, you can also specify up to 5 source files (that is, a total of 6 fragments)// All source files must belong to the same storage space//format:avconcat/<mode>/format/<format>/<encodedurl0>/<encodedurl1>/<
encodedurl2>/... String ENCODEDURL1 = Urlsafebase64.encodetostring ("http://7xl4c9.com1.z0.glb.clouddn.com/pingjie2.flv");
String encodedUrl2 = urlsafebase64.encodetostring ("Http://7xl4c9.com1.z0.glb.clouddn.com/pingjie3.avi");

FoPs = "avconcat/2/format/mp4/" +ENCODEDURL1+ENCODEDURL2; ------------------Multi-File Compression-------------------//can store resource files in several seven-cow spaces in seven-cow-end compressed Storage//Format:mkzip/<mode>/url/< base64encodedurl>/alias/<base64encodedalias>/url/<base64encodedurl> String encodedfile1 =
Urlsafebase64.encodetostring ("http://7xl4c9.com1.z0.glb.clouddn.com/photo1.jpg");
String encodedfile2 = urlsafebase64.encodetostring ("Http://7xl4c9.com1.z0.glb.clouddn.com/vedio1.mp4");
String encodedfile3 = urlsafebase64.encodetostring ("Http://7xl4c9.com1.z0.glb.clouddn.com/audio1.mp3"); FoPs = "mkzip/2/url/" +encodedfile1+ "url/" +encodedfile2+ "url/" +ENCODEDFILE3;
File Download

Build Download Link

Import Com.qiniu.util.Auth;
public class Downloaddemo {
   //Set account Access_key and Secret_key
   String access_key = "Access_key";
   String Secret_key = "Secret_key";
   Key configuration
   Auth Auth = auth.create (Access_key, secret_key);
   Constructs the private space the need to generate the downloaded link
   String URL = "Http://bucketdomain/key";

   The public void Download () {
     //calls the Privatedownloadurl method to generate the download link, and the second parameter can set the token expiration
     String Downloadrul = Auth.privatedownloadurl (url,3600);
     System.out.println (Downloadrul);
   }
   public static void Main (String args[]) {
     new Downloaddemo (). Download ();
   }
}
Management of space resources

getting information about a file

Import com.qiniu.common.QiniuException;
Import Com.qiniu.http.Response;
Import Com.qiniu.storage.BucketManager;
Import Com.qiniu.storage.model.FileInfo;
Import Com.qiniu.util.Auth;

public class Bucketmanagerdemo {public

  static void Main (String args[]) {
    //Set the AK and SK String for the account to be operated
    Access_ KEY = "Access_key";
    String Secret_key = "Secret_key";
    Auth Auth = auth.create (Access_key, secret_key);
    Instantiate a Bucketmanager object
    bucketmanager bucketmanager = new Bucketmanager (auth);
    To test the space and key, and the key in your space exists
    String bucket = "Bucket_name";
    String key = "Bucket_key";
    try {
      //Call Stat () method to get the information of the file
      FileInfo info = bucketmanager.stat (bucket, key);
      System.out.println (Info.hash);
      System.out.println (Info.key);
    } catch (Qiniuexception e) {
      //catch exception information
      Response r = e.response;
      System.out.println (R.tostring ());}}

move a single file

import com.qiniu.common.QiniuException; import com.qiniu.http.Response; import
Com.qiniu.storage.BucketManager;
Import Com.qiniu.storage.model.FileInfo;

Import Com.qiniu.util.Auth; public class Bucketmanagerdemo {public static void main (String args[] 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.