BOSClientUtil.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.jeeplus.common.bos;
  2. import com.baidubce.auth.DefaultBceCredentials;
  3. import com.baidubce.services.bos.BosClient;
  4. import com.baidubce.services.bos.BosClientConfiguration;
  5. import com.baidubce.services.bos.model.BosObject;
  6. import com.baidubce.services.bos.model.PutObjectResponse;
  7. import com.jeeplus.common.config.Global;
  8. import org.junit.Test;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.UnsupportedEncodingException;
  13. import java.net.URL;
  14. import java.net.URLDecoder;
  15. import java.net.URLEncoder;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. public class BOSClientUtil {
  19. private final String accessKey= Global.getConfig("bos_access_key");
  20. private final String secretKey= Global.getConfig("bos_secret_key");
  21. private final String endpoint= Global.getConfig("bos_endpoint");
  22. private final String bucketName= Global.getConfig("bos_buck_name");
  23. private final String urlTem = "https://BUCKNAME.su.bcebos.comKEY";
  24. private BosClient bosClient;
  25. public BOSClientUtil() {
  26. BosClientConfiguration config = new BosClientConfiguration();
  27. config.setMaxConnections(10);
  28. config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
  29. config.setEndpoint(endpoint);
  30. bosClient = new BosClient(config);
  31. }
  32. /**
  33. * 获取BosClient对象
  34. * @return
  35. */
  36. public BosClient getBosClient() {
  37. BosClientConfiguration config = new BosClientConfiguration();
  38. config.setMaxConnections(10);
  39. config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
  40. config.setEndpoint(endpoint);
  41. return new BosClient(config);
  42. }
  43. /**
  44. *百度bos以file形式上传文件(不超过5GB)
  45. * @param file
  46. * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
  47. * @return
  48. */
  49. public PutObjectResponse uploadFileToBos(File file,
  50. String objectKey) {
  51. return bosClient.putObject(bucketName, objectKey, file);
  52. }
  53. /**
  54. *以数据流形式上传Object(不超过5GB)
  55. * @param inputStream
  56. * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
  57. * @return
  58. */
  59. public PutObjectResponse uploadInputStreamToBos(InputStream inputStream,
  60. String objectKey) {
  61. return bosClient.putObject(bucketName, objectKey, inputStream);
  62. }
  63. /**
  64. * 删除已经上传的Object
  65. *
  66. * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
  67. * @return 上传成功后的tag
  68. */
  69. public void deleteObject(String objectKey) {
  70. bosClient.deleteObject(bucketName, objectKey);
  71. }
  72. /**
  73. * 获取文件下载URL
  74. * @param objectKey 文件夹和文件名
  75. * @return 目标文件的下载url
  76. */
  77. public String generatePresignedUrl(String objectKey) {
  78. URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
  79. return url.toString();
  80. }
  81. public InputStream getObject(String objectKey)
  82. throws IOException {
  83. // 获取Object,返回结果为BosObject对象
  84. BosObject object = bosClient.getObject(bucketName, objectKey);
  85. // 获取Object的输入流
  86. InputStream inputStream = object.getObjectContent();
  87. return inputStream;
  88. }
  89. public String upload(String path,File file){
  90. String objectKey = path+file.getName();
  91. bosClient.putObject(bucketName, objectKey, file);
  92. String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",objectKey);
  93. // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
  94. return url;
  95. }
  96. public String upload(String path,InputStream inputStream){
  97. bosClient.putObject(bucketName, path, inputStream);
  98. String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",path);
  99. // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
  100. return url;
  101. }
  102. // @Test
  103. // public void testPut(){
  104. // File file = new File("E:/bk1.png");
  105. // String name = file.getName();
  106. // BOSClientUtil.uploadFileToBos(file,"/test/"+name);
  107. // }
  108. //
  109. //
  110. // @Test
  111. // public void testDel(){
  112. // BOSClientUtil.deleteObject("/test/1.txt");
  113. // }
  114. //
  115. // @Test
  116. // public void testUrl(){
  117. // File file = new File("E:/bk1.png");
  118. // String upload = BOSClientUtil.upload("/text1/", file);
  119. // System.out.println(upload);
  120. // }
  121. // @Test
  122. // public void uploadAll(){
  123. // ArrayList<String> listFileName = new ArrayList<String>();
  124. // String filePath = "D:/IDEA workspace/total_process/src/main/webapp/static/";
  125. // getAllFileName(filePath,listFileName);
  126. // int count = 1;
  127. // int total = listFileName.size();
  128. // System.out.println(total);
  129. // for(String name : listFileName){
  130. // if(name != null) {
  131. // int statice = name.indexOf("static");
  132. // String name1 = name.substring(statice + 6, name.length());
  133. // File file = new File(name);
  134. //// System.out.println("/static"+name1);
  135. // bosClient.putObject(bucketName, "/static"+name1, file);
  136. //// BOSClientUtil.upload("/static"+name1,file);
  137. // System.out.println("---------正在上传 "+file.getName()+"-------");
  138. // System.out.println("上传第"+count+"个资源,共"+total+"个资源");
  139. // count++;
  140. //// try {
  141. //// bosClient.deleteObject(bucketName, " /static" + name1);
  142. //// }catch (Exception e){
  143. //// e.printStackTrace();
  144. //// }
  145. // }
  146. // }
  147. // }
  148. public void getAllFileName(String path, ArrayList<String> listFileName){
  149. File file = new File(path);
  150. File [] files = file.listFiles();
  151. String [] names = file.list();
  152. if(names != null){
  153. String [] completNames = new String[names.length];
  154. for(int i=0;i<names.length;i++){
  155. if(files[i].isFile()) {
  156. completNames[i] = path + names[i];
  157. }
  158. }
  159. listFileName.addAll(Arrays.asList(completNames));
  160. }
  161. for(File a:files){
  162. if(a.isDirectory()){//如果文件夹下有子文件夹,获取子文件夹下的所有文件全路径。
  163. getAllFileName(a.getAbsolutePath().replaceAll("\\\\","/")+"/",listFileName);
  164. }
  165. }
  166. }
  167. }