支持以下类型:

  1. String(filepath): /storage/emulated/0/Pictures/Screenshots/Screenshot_20201017_085301_com.ss.android.ugc.aweme.jpg
  2. Uri(fileUri): content://media/external/file/404557
  1. implementation 'androidx.appcompat:appcompat:1.2.0'
  2. implementation 'me.luzhuo.android:lib_file:1.1.0-SNAPSHOT'
  3. implementation 'me.luzhuo.android:lib_oss:1.0.15-SNAPSHOT'

1. 使用

1. 初始化

  1. private OSSUtils ossUtils;
  2. private String FILE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. ossUtils = OSSUtils.getInstance(this);
  8. }

2. 上传单个文件

  1. /**
  2. * 上传单个文件
  3. */
  4. public void uploadFile(View view) {
  5. OSSFileBean fileBean = new OSSFileBean(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
  6. ossUtils.uploadFileByAliOSS(fileBean, new IProgress() {
  7. @Override
  8. public void onProgress(int progress, long currentSize, long totalSize) {
  9. Log.e(TAG, "" + progress + " : " + currentSize + " : " + totalSize);
  10. }
  11. }, new IOSSFileCallback() {
  12. @Override
  13. public void onSuccess(OSSFileBean fileBean) {
  14. Log.e(TAG, "" + fileBean.getPath());
  15. }
  16. @Override
  17. public void onError(String s) {
  18. Log.e(TAG, "" + s);
  19. }
  20. });
  21. }

3. 上传多个文件

  1. /**
  2. * 上传多个文件
  3. */
  4. public void uploadFiles(View view) {
  5. List files = new ArrayList<OSSFileBean>();
  6. for (int i = 0; i <= 5; i++){
  7. files.add(new OSSFileBean(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-", "")));
  8. }
  9. Log.e(TAG, "开始上传多个文件");
  10. ossUtils.uploadFilesByAliOSS(files, new IProgress() {
  11. @Override
  12. public void onProgress(int progress, long currentSize, long totalSize) {
  13. Log.e(TAG, "" + progress + " : " + currentSize + " : " + totalSize);
  14. }
  15. }, new IOSSFilesCallback() {
  16. @Override
  17. public void onSuccess(List<OSSFileBean> fileBeans) {
  18. for (OSSFileBean fileBean : fileBeans) {
  19. Log.e(TAG, "" + fileBean.getPath());
  20. }
  21. }
  22. @Override
  23. public void onError(String s) {
  24. Log.e(TAG, "" + s);
  25. }
  26. });
  27. }

2. 工具类

OSSUtils.java

  1. /* Copyright 2020 Luzhuo. All rights reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package me.luzhuo.ossdemo;
  16. import android.content.Context;
  17. import com.google.gson.Gson;
  18. import java.util.List;
  19. import java.util.concurrent.ExecutorService;
  20. import java.util.concurrent.Executors;
  21. import me.luzhuo.lib_okhttp.IOKHttpManager;
  22. import me.luzhuo.lib_okhttp.OKHttpManager;
  23. import me.luzhuo.lib_okhttp.interceptor.TokenInterceptor;
  24. import me.luzhuo.lib_oss.ALiOSSFileServer;
  25. import me.luzhuo.lib_oss.IALiOSSFileServer;
  26. import me.luzhuo.lib_oss.bean.OSSFileBean;
  27. import me.luzhuo.lib_oss.bean.OSSSTSBean;
  28. import me.luzhuo.lib_oss.callback.IOSSFileCallback;
  29. import me.luzhuo.lib_oss.callback.IOSSFilesCallback;
  30. import me.luzhuo.lib_oss.callback.IProgress;
  31. /**
  32. * Description:
  33. *
  34. * @Author: Luzhuo
  35. * @Creation Date: 2020/8/2 11:21
  36. * @Copyright: Copyright 2020 Luzhuo. All rights reserved.
  37. **/
  38. public class OSSUtils {
  39. private static OSSUtils instance;
  40. private IALiOSSFileServer ossFileServer;
  41. private IOKHttpManager okHttpManager;
  42. private ExecutorService threadpool = Executors.newCachedThreadPool();
  43. private static final String STSServer = "https://api-chongjia.jincaishuizu.com/find/app/v1/pic/getAliCloudSupposeAcc";
  44. private static final String EndPoint = "cj-oss.jcprod.xyz";
  45. private static final String BucketName = "chongjia";
  46. private static final String prefix = "0001/";
  47. public static OSSUtils getInstance(Context context) {
  48. if (instance == null){
  49. synchronized (OSSUtils.class){
  50. if (instance == null){
  51. instance = new OSSUtils(context);
  52. }
  53. }
  54. }
  55. return instance;
  56. }
  57. private OSSUtils(Context context) {
  58. try {
  59. okHttpManager = new OKHttpManager(
  60. new TokenInterceptor() {
  61. @Override
  62. public String getToken() {
  63. return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzAyMyIsImlhdCI6MTU5NjM0NTY2OSwiZXhwIjoxNTk2OTUwNDY5fQ.Jo3aL7v5qNFg6ybCqxARsC41znKY7XBbfHqdLYt8yWK2T1186dnf3dGQ7AUujRH0MwZ68M7aKiLTVQ_uGZF54Q";
  64. }
  65. });
  66. }catch (Exception e){
  67. e.printStackTrace();
  68. }
  69. ossFileServer = new ALiOSSFileServer(context.getApplicationContext(), STSServer, EndPoint, BucketName, prefix) {
  70. @Override
  71. protected OSSSTSBean getTokenFromUser(String s) {
  72. try {
  73. String message = okHttpManager.get(STSServer);
  74. Gson gson = new Gson();
  75. STSBean bean = gson.fromJson(message, STSBean.class);
  76. if(bean.code != 200){
  77. return null;
  78. }
  79. STSBean.DataBean dataBean = bean.data;
  80. return new OSSSTSBean(dataBean.AccessKeyId, dataBean.AccessKeySecret, dataBean.SecurityToken, dataBean.Expiration);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. }
  84. return null;
  85. }
  86. };
  87. }
  88. /**
  89. * 使用案例
  90. *
  91. * <pre>
  92. * ManagerUser.getInstance(this).setToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzMzE2MyIsImlhdCI6MTU4OTcyODc4NCwiZXhwIjoxNTkwMzMzNTg0fQ.FRHKWh-siG40NqG3qrtGl3Gb_vS3HjBFRrSRqNfEtzUA9ckoCDcwwi11wiB0WTQCrMhGACqZZfg26jeItIrO4g");
  93. * String FILE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
  94. * FileBean fileBean = new FileBean();
  95. * fileBean.setPath2AliOSS(FILE_DIR + "wangwang.zip", UUID.randomUUID().toString().replace("-",""));
  96. * ManagetNetWork.getInstance(this).uploadFileByAliOSS(fileBean, new IProgress() {
  97. * @Override
  98. * public void onProgress(int i, long l, long l1) {
  99. * Log.e(TAG, "" + i + " : " + l + " : " + l1);
  100. * }
  101. * }, new FileWorkCallbackImpl() {
  102. * @Override
  103. * public void onSuccess(FileBean fileBean) {
  104. * Log.e(TAG, "" + fileBean.getPath2AliOSS());
  105. * }
  106. *
  107. * @Override
  108. * public void onError(String s) {
  109. * Log.e(TAG, "" + s);
  110. * }
  111. * });
  112. * </pre>
  113. *
  114. * @param fileBean
  115. * @param progress
  116. * @param callback
  117. */
  118. public void uploadFileByAliOSS(final OSSFileBean fileBean, final IProgress progress, final IOSSFileCallback callback) {
  119. threadpool.execute(new Runnable() {
  120. @Override
  121. public void run() {
  122. ossFileServer.uploadFileByAliOSS(fileBean, progress, callback);
  123. }
  124. });
  125. }
  126. public void uploadFilesByAliOSS(final List<OSSFileBean> fileBeans, final IProgress iProgress, final IOSSFilesCallback callback) {
  127. threadpool.execute(new Runnable() {
  128. @Override
  129. public void run() {
  130. ossFileServer.uploadFilesByAliOSS(fileBeans, iProgress, callback);
  131. }
  132. });
  133. }
  134. }