123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package com.jeeplus.common.bos;
- import com.baidubce.auth.DefaultBceCredentials;
- import com.baidubce.services.bos.BosClient;
- import com.baidubce.services.bos.BosClientConfiguration;
- import com.baidubce.services.bos.model.BosObject;
- import com.baidubce.services.bos.model.PutObjectResponse;
- import com.jeeplus.common.config.Global;
- import org.junit.Test;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class BOSClientUtil {
- private final String accessKey= Global.getConfig("bos_access_key");
- private final String secretKey= Global.getConfig("bos_secret_key");
- private final String endpoint= Global.getConfig("bos_endpoint");
- private final String bucketName= Global.getConfig("bos_buck_name");
- private final String urlTem = "https://BUCKNAME.su.bcebos.comKEY";
- private BosClient bosClient;
- public BOSClientUtil() {
- BosClientConfiguration config = new BosClientConfiguration();
- config.setMaxConnections(10);
- config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
- config.setEndpoint(endpoint);
- bosClient = new BosClient(config);
- }
- /**
- * 获取BosClient对象
- * @return
- */
- public BosClient getBosClient() {
- BosClientConfiguration config = new BosClientConfiguration();
- config.setMaxConnections(10);
- config.setCredentials(new DefaultBceCredentials(accessKey, secretKey));
- config.setEndpoint(endpoint);
- return new BosClient(config);
- }
- /**
- *百度bos以file形式上传文件(不超过5GB)
- * @param file
- * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
- * @return
- */
- public PutObjectResponse uploadFileToBos(File file,
- String objectKey) {
- return bosClient.putObject(bucketName, objectKey, file);
- }
- /**
- *以数据流形式上传Object(不超过5GB)
- * @param inputStream
- * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
- * @return
- */
- public PutObjectResponse uploadInputStreamToBos(InputStream inputStream,
- String objectKey) {
- return bosClient.putObject(bucketName, objectKey, inputStream);
- }
- /**
- * 删除已经上传的Object
- *
- * @param objectKey 文件路径/文件名(可以用“/”来创建多层文件夹)
- * @return 上传成功后的tag
- */
- public void deleteObject(String objectKey) {
- bosClient.deleteObject(bucketName, objectKey);
- }
- /**
- * 获取文件下载URL
- * @param objectKey 文件夹和文件名
- * @return 目标文件的下载url
- */
- public String generatePresignedUrl(String objectKey) {
- URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
- return url.toString();
- }
- public InputStream getObject(String objectKey)
- throws IOException {
- // 获取Object,返回结果为BosObject对象
- BosObject object = bosClient.getObject(bucketName, objectKey);
- // 获取Object的输入流
- InputStream inputStream = object.getObjectContent();
- return inputStream;
- }
- public String upload(String path,File file){
- String objectKey = path+file.getName();
- bosClient.putObject(bucketName, objectKey, file);
- String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",objectKey);
- // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
- return url;
- }
- public String upload(String path,InputStream inputStream){
- bosClient.putObject(bucketName, path, inputStream);
- String url = urlTem.replace("BUCKNAME",bucketName).replace("KEY",path);
- // URL url = bosClient.generatePresignedUrl(bucketName, objectKey, -1);
- return url;
- }
- // @Test
- // public void testPut(){
- // File file = new File("E:/bk1.png");
- // String name = file.getName();
- // BOSClientUtil.uploadFileToBos(file,"/test/"+name);
- // }
- //
- //
- // @Test
- // public void testDel(){
- // BOSClientUtil.deleteObject("/test/1.txt");
- // }
- //
- // @Test
- // public void testUrl(){
- // File file = new File("E:/bk1.png");
- // String upload = BOSClientUtil.upload("/text1/", file);
- // System.out.println(upload);
- // }
- // @Test
- // public void uploadAll(){
- // ArrayList<String> listFileName = new ArrayList<String>();
- // String filePath = "D:/IDEA workspace/total_process/src/main/webapp/static/";
- // getAllFileName(filePath,listFileName);
- // int count = 1;
- // int total = listFileName.size();
- // System.out.println(total);
- // for(String name : listFileName){
- // if(name != null) {
- // int statice = name.indexOf("static");
- // String name1 = name.substring(statice + 6, name.length());
- // File file = new File(name);
- //// System.out.println("/static"+name1);
- // bosClient.putObject(bucketName, "/static"+name1, file);
- //// BOSClientUtil.upload("/static"+name1,file);
- // System.out.println("---------正在上传 "+file.getName()+"-------");
- // System.out.println("上传第"+count+"个资源,共"+total+"个资源");
- // count++;
- //// try {
- //// bosClient.deleteObject(bucketName, " /static" + name1);
- //// }catch (Exception e){
- //// e.printStackTrace();
- //// }
- // }
- // }
- // }
- public void getAllFileName(String path, ArrayList<String> listFileName){
- File file = new File(path);
- File [] files = file.listFiles();
- String [] names = file.list();
- if(names != null){
- String [] completNames = new String[names.length];
- for(int i=0;i<names.length;i++){
- if(files[i].isFile()) {
- completNames[i] = path + names[i];
- }
- }
- listFileName.addAll(Arrays.asList(completNames));
- }
- for(File a:files){
- if(a.isDirectory()){//如果文件夹下有子文件夹,获取子文件夹下的所有文件全路径。
- getAllFileName(a.getAbsolutePath().replaceAll("\\\\","/")+"/",listFileName);
- }
- }
- }
- }
|