|
@@ -0,0 +1,907 @@
|
|
|
+package com.jeeplus.common.oss;
|
|
|
+
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.OSSException;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import com.jeeplus.common.config.Global;
|
|
|
+import com.jeeplus.common.utils.Encodes;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.MappedByteBuffer;
|
|
|
+import java.nio.channels.FileChannel;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by Meng on 2017/6/8.
|
|
|
+ */
|
|
|
+public class OSSClientUtil {
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(OSSClientUtil.class);
|
|
|
+ private String appData = Global.getAppData();
|
|
|
+ private String endpoint = Global.getEndpoint();
|
|
|
+ private String accessKeyId = Global.getAccessKeyId();
|
|
|
+ private String accessKeySecret = Global.getAccessKeySecret();
|
|
|
+ private String bucketName = Global.getBucketName();
|
|
|
+ private String savePath = Global.getAliyunUrl();
|
|
|
+ private String avatarDir = Global.getAvatarDir();
|
|
|
+ private String notifyDir = Global.getNotifyDir();
|
|
|
+ private String reportDir = Global.getReportDir();
|
|
|
+ private String rqcode = Global.getRqcode();
|
|
|
+ private String goout = Global.getGoout();
|
|
|
+ private String leave = Global.getLeave();
|
|
|
+ private String overtimeform = Global.getOvertimeform();
|
|
|
+ private String sealform = Global.getSealform();
|
|
|
+ private String workReimbur = Global.getWorkReimbur();
|
|
|
+ private String evection = Global.getEvection();
|
|
|
+ private String photo = Global.getPhoto();
|
|
|
+ private String userEvaluation = Global.getUserEvaluation();
|
|
|
+ private String workBidingDocument = Global.getWorkBidingDocument();
|
|
|
+ private String workEngineeringProject = Global.getWorkEngineeringProject();
|
|
|
+ private String workFullExecute = Global.getWorkFullExecute();
|
|
|
+ private String workFullMeetingminutes = Global.getWorkFullMeetingminutes();
|
|
|
+ private String workFullDesignchange = Global.getWorkFullDesignchange();
|
|
|
+ private String workFullConstructsheet = Global.getWorkFullConstructsheet();
|
|
|
+ private String workFullProprietorsheet = Global.getWorkFullProprietorsheet();
|
|
|
+ private String workFullSupervisorsheet = Global.getWorkFullSupervisorsheet();
|
|
|
+ private String workProjectReport = Global.getWorkProjectReport();
|
|
|
+ private String workProjectBasis = Global.getWorkProjectBasis();
|
|
|
+ private String workProjectRemote = Global.getWorkProjectRemote();
|
|
|
+ private String workProjectSummary = Global.getWorkProjectSummary();
|
|
|
+ private String workProjectOther = Global.getWorkProjectOther();
|
|
|
+ private String workVisa = Global.getWorkVisa();
|
|
|
+ private String Officehonor = Global.getOfficehonor();
|
|
|
+ private String jobResume = Global.getJobResume();
|
|
|
+ private String satisfaction = Global.getSatisfaction();
|
|
|
+ private String certificate = Global.getCertificate();
|
|
|
+ private String judgeAttachment = Global.getJudgeAttachment();
|
|
|
+ private String oaBuy = Global.getOaBuy();
|
|
|
+ private String oaAll = Global.getOaAll();
|
|
|
+ private String im = Global.getIm();
|
|
|
+ private String workContractInfo = Global.getWorkContractInfo();
|
|
|
+ private String qzBucketName = Global.getQzBucketName();;
|
|
|
+ private OSSClient ossClient;
|
|
|
+
|
|
|
+ public OSSClientUtil() {
|
|
|
+ ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化
|
|
|
+ */
|
|
|
+ public void init() {
|
|
|
+ ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 销毁
|
|
|
+ */
|
|
|
+ public void destroy() {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ *
|
|
|
+ * @param fileFile 文件
|
|
|
+ * @param type 文件类型 avatar 头像;notify公告
|
|
|
+ */
|
|
|
+ public String uploadFile2OSS(MultipartFile fileFile,String type){
|
|
|
+ switch (type){
|
|
|
+ case "appData":
|
|
|
+ return uploadImg2OSS(fileFile,appData);
|
|
|
+ case "oaBuy":
|
|
|
+ return uploadImg2OSS(fileFile,oaBuy);
|
|
|
+ case "oaAll":
|
|
|
+ return uploadImg2OSS(fileFile,oaAll);
|
|
|
+ case "avatar":
|
|
|
+ return uploadImg2OSS(fileFile,avatarDir);
|
|
|
+ case "notify":
|
|
|
+ return uploadImg2OSS(fileFile,notifyDir);
|
|
|
+ case "report":
|
|
|
+ return uploadImg2OSS(fileFile,reportDir);
|
|
|
+ case "rqcode":
|
|
|
+ return uploadImg2OSS(fileFile,rqcode);
|
|
|
+ case "goout":
|
|
|
+ return uploadImg2OSS(fileFile,goout);
|
|
|
+ case "leave":
|
|
|
+ return uploadImg2OSS(fileFile,leave);
|
|
|
+ case "overtimeform":
|
|
|
+ return uploadImg2OSS(fileFile,overtimeform);
|
|
|
+ case "sealform":
|
|
|
+ return uploadImg2OSS(fileFile,sealform);
|
|
|
+ case "workReimbur":
|
|
|
+ return uploadImg2OSS(fileFile,workReimbur);
|
|
|
+ case "evection":
|
|
|
+ return uploadImg2OSS(fileFile,evection);
|
|
|
+ case "photo":
|
|
|
+ return uploadImg2OSS(fileFile,photo);
|
|
|
+ case "userEvaluation" :
|
|
|
+ return uploadImg2OSS(fileFile,userEvaluation);
|
|
|
+ case "workBidingDocument":
|
|
|
+ return uploadImg2OSS(fileFile,workBidingDocument);
|
|
|
+ case "workEngineeringProject":
|
|
|
+ return uploadImg2OSS(fileFile,workEngineeringProject);
|
|
|
+ case "workFullExecute":
|
|
|
+ return uploadImg2OSS(fileFile,workFullExecute);
|
|
|
+ case "workFullMeetingminutes":
|
|
|
+ return uploadImg2OSS(fileFile,workFullMeetingminutes);
|
|
|
+ case "workFullDesignchange":
|
|
|
+ return uploadImg2OSS(fileFile,workFullDesignchange);
|
|
|
+ case "workFullConstructsheet":
|
|
|
+ return uploadImg2OSS(fileFile,workFullConstructsheet);
|
|
|
+ case "workFullProprietorsheet":
|
|
|
+ return uploadImg2OSS(fileFile,workFullProprietorsheet);
|
|
|
+ case "workFullSupervisorsheet":
|
|
|
+ return uploadImg2OSS(fileFile,workFullSupervisorsheet);
|
|
|
+ case "workProjectReport":
|
|
|
+ return uploadImg2OSS(fileFile,workProjectReport);
|
|
|
+ case "workProjectBasis":
|
|
|
+ return uploadImg2OSS(fileFile,workProjectBasis);
|
|
|
+ case "workProjectRemote":
|
|
|
+ return uploadImg2OSS(fileFile,workProjectRemote);
|
|
|
+ case "workProjectSummary":
|
|
|
+ return uploadImg2OSS(fileFile,workProjectSummary);
|
|
|
+ case "workProjectOther":
|
|
|
+ return uploadImg2OSS(fileFile,workProjectOther);
|
|
|
+ case "workVisa":
|
|
|
+ return uploadImg2OSS(fileFile,workVisa);
|
|
|
+ case "Officehonor":
|
|
|
+ return uploadImg2OSS(fileFile,Officehonor);
|
|
|
+ case "jobResume":
|
|
|
+ return uploadImg2OSS(fileFile,jobResume);
|
|
|
+ case "satisfaction":
|
|
|
+ return uploadImg2OSS(fileFile,satisfaction);
|
|
|
+ case "certificate":
|
|
|
+ return uploadImg2OSS(fileFile,certificate);
|
|
|
+ case "judgeAttachment":
|
|
|
+ return uploadImg2OSS(fileFile,judgeAttachment);
|
|
|
+ case "im":
|
|
|
+ return uploadImg2OSS(fileFile,im);
|
|
|
+ case "workContractInfo":
|
|
|
+ return uploadImg2OSS(fileFile,workContractInfo);
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 上传文件
|
|
|
+// *
|
|
|
+// * @param url
|
|
|
+// */
|
|
|
+// public void uploadImg2OSS(String url) {
|
|
|
+// File fileOnServer = new File(url);
|
|
|
+// FileInputStream fin;
|
|
|
+// try {
|
|
|
+// fin = new FileInputStream(fileOnServer);
|
|
|
+// String[] split = url.split("/");
|
|
|
+// this.uploadFile2OSS(fin,this/filedir, split[split.length - 1]);
|
|
|
+// } catch (FileNotFoundException e) {
|
|
|
+// System.out.println("文件上传失败");
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ public String uploadImg2OSS(MultipartFile file,String fileDir) {
|
|
|
+ if (file.getSize() > 10 * 1024 * 1024) {
|
|
|
+ System.out.println("上传文件大小不能超过10M!");
|
|
|
+ return "上传文件大小不能超过10M!";
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
+ String fileName="";
|
|
|
+ fileDir = fileDir+System.nanoTime()+"/";
|
|
|
+ String md5 ="";
|
|
|
+ File f = null;
|
|
|
+ try {
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ f=File.createTempFile("tmp", null);
|
|
|
+ file.transferTo(f);
|
|
|
+ f.deleteOnExit();
|
|
|
+ md5 = getMd5ByFile(f);
|
|
|
+ System.out.println(md5);
|
|
|
+ fileName= md5+ substring;
|
|
|
+ System.out.println(fileName);
|
|
|
+ this.uploadFile2OSS(inputStream,fileDir, fileName);
|
|
|
+ }catch (IOException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }catch (Exception e1){
|
|
|
+ e1.printStackTrace();
|
|
|
+ System.out.println("文件上传失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ String name = savePath+"/"+fileDir+fileName;
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String uploadImg2OSSAndroid(MultipartFile file,String fileDir) {
|
|
|
+ /*if (file.getSize() > 10 * 1024 * 1024) {
|
|
|
+ System.out.println("上传文件大小不能超过10M!");
|
|
|
+ return "上传文件大小不能超过10M!";
|
|
|
+ }*/
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
+ String fileName="";
|
|
|
+ fileDir = fileDir+System.nanoTime()+"/";
|
|
|
+ String md5 ="";
|
|
|
+ File f = null;
|
|
|
+ try {
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ f=File.createTempFile("tmp", null);
|
|
|
+ file.transferTo(f);
|
|
|
+ f.deleteOnExit();
|
|
|
+ md5 = getMd5ByFile(f);
|
|
|
+ System.out.println(md5);
|
|
|
+ fileName= md5+ substring;
|
|
|
+ System.out.println(fileName);
|
|
|
+ this.uploadFile2OSSAndroid(inputStream,fileDir, fileName);
|
|
|
+ }catch (IOException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }catch (Exception e1){
|
|
|
+ e1.printStackTrace();
|
|
|
+ System.out.println("文件上传失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ String name = savePath+"/"+fileDir+fileName;
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传到OSS服务器 如果同名文件会覆盖服务器上的
|
|
|
+ *
|
|
|
+ * @param fileName 文件名称 包括后缀名
|
|
|
+ * @return 出错返回"" ,唯一MD5数字签名
|
|
|
+ */
|
|
|
+ public String uploadFile2OSS(InputStream inStream,String fileDir, String fileName) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ //上传文件
|
|
|
+ PutObjectResult putResult = ossClient.putObject(bucketName, fileDir + fileName, inStream);
|
|
|
+ String ret = putResult.getETag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (inStream != null) {
|
|
|
+ inStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("上传文件到云服务器成功,文件名:{},耗时:{}ms",fileName,end-start);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ *Android安卓固定下载地址
|
|
|
+ * @param fileName 文件名称 包括后缀名
|
|
|
+ * @return 出错返回"" ,唯一MD5数字签名
|
|
|
+ */
|
|
|
+ public String uploadFile2OSSAndroid(InputStream inStream,String fileDir, String fileName) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ try {
|
|
|
+ while ((len = inStream.read(buffer)) > -1 ) {
|
|
|
+ baos.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ baos.flush();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ //上传文件
|
|
|
+ PutObjectResult putResult = ossClient.putObject(bucketName, fileDir + fileName, is1);
|
|
|
+ ossClient.putObject(bucketName, Global.getVersion()+"ruihuaoa.apk", is2);
|
|
|
+ //ossClient.putObject(bucketName, Global.getTestVersion()+"ruihuaoa.apk", is2);//测试用
|
|
|
+ String ret = putResult.getETag();
|
|
|
+
|
|
|
+ try {
|
|
|
+ if (inStream != null) {
|
|
|
+ is1.close();
|
|
|
+ is2.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("上传文件到云服务器成功,文件名:{},耗时:{}ms",fileName,end-start);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void uploadFile2OSSToo(String key, String url) throws Throwable {
|
|
|
+ // 设置断点续传请求
|
|
|
+ UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
|
|
|
+ // 指定上传的本地文件
|
|
|
+ uploadFileRequest.setUploadFile(url);
|
|
|
+ // 指定上传并发线程数
|
|
|
+ uploadFileRequest.setTaskNum(5);
|
|
|
+ // 指定上传的分片大小
|
|
|
+ uploadFileRequest.setPartSize(1 * 1024 * 1024);
|
|
|
+ // 开启断点续传
|
|
|
+ uploadFileRequest.setEnableCheckpoint(true);
|
|
|
+ // 断点续传上传
|
|
|
+ UploadFileResult result = ossClient.uploadFile(uploadFileRequest);
|
|
|
+ System.out.println("233--"+result);
|
|
|
+ // 关闭client
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUploadId(String key){
|
|
|
+ InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
|
|
|
+ InitiateMultipartUploadResult result = ossClient.initiateMultipartUpload(request);
|
|
|
+ String uploadId = result.getUploadId();
|
|
|
+ return uploadId;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *获取已上传的分片
|
|
|
+ * UploadId,initiateMultipartUpload返回的结果获取
|
|
|
+ */
|
|
|
+ public void uploadPartListing (String key){
|
|
|
+ String uploadId = getUploadId(key);
|
|
|
+ ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
|
|
|
+ PartListing partListing = ossClient.listParts(listPartsRequest);
|
|
|
+ for (PartSummary part : partListing.getParts()) {
|
|
|
+ // 分片号,上传时候指定
|
|
|
+ part.getPartNumber();
|
|
|
+ // 分片数据大小
|
|
|
+ part.getSize();
|
|
|
+ // Part的ETag
|
|
|
+ part.getETag();
|
|
|
+ // Part的最后修改上传
|
|
|
+ part.getLastModified();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传分片
|
|
|
+ */
|
|
|
+ public void uploadSetEnableCheckpoint(String key,String localFile){
|
|
|
+ List<PartETag> partETags = new ArrayList<PartETag>();
|
|
|
+ InputStream instream = null;
|
|
|
+ try {
|
|
|
+ instream = new FileInputStream(new File(localFile));
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ UploadPartRequest uploadPartRequest = new UploadPartRequest();
|
|
|
+ uploadPartRequest.setBucketName(bucketName);
|
|
|
+ uploadPartRequest.setKey(key);
|
|
|
+ String uploadId = "7188157413A24508B8E327913AFBFCCE";
|
|
|
+ uploadPartRequest.setUploadId(uploadId);
|
|
|
+ uploadPartRequest.setInputStream(instream);
|
|
|
+ // 设置分片大小,除最后一个分片外,其它分片要大于100KB
|
|
|
+ uploadPartRequest.setPartSize(100 * 1024);
|
|
|
+ // 设置分片号,范围是1~10000,
|
|
|
+ uploadPartRequest.setPartNumber(1);
|
|
|
+ UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
|
|
|
+ partETags.add(uploadPartResult.getPartETag());
|
|
|
+ completeMultipartUpload(key,partETags);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 完成分片上传
|
|
|
+ */
|
|
|
+ public void completeMultipartUpload(String key,List<PartETag> partETags){
|
|
|
+ String uploadId = "490C8D60323F4AE3ACA2C627A785902A";
|
|
|
+ Collections.sort(partETags, new Comparator<PartETag>() {
|
|
|
+ @Override
|
|
|
+ public int compare(PartETag p1, PartETag p2) {
|
|
|
+ return p1.getPartNumber() - p2.getPartNumber();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ CompleteMultipartUploadRequest completeMultipartUploadRequest =
|
|
|
+ new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
|
|
|
+ ossClient.completeMultipartUpload(completeMultipartUploadRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消分片上传事件
|
|
|
+ */
|
|
|
+ public void abortMultipartUpload(String key){
|
|
|
+ // 取消分片上传,其中uploadId来自于initiateMultipartUpload
|
|
|
+ String uploadId = "490C8D60323F4AE3ACA2C627A785902A";
|
|
|
+ AbortMultipartUploadRequest abortMultipartUploadRequest =
|
|
|
+ new AbortMultipartUploadRequest(bucketName, key, uploadId);
|
|
|
+ ossClient.abortMultipartUpload(abortMultipartUploadRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取已上传的分片
|
|
|
+ */
|
|
|
+ public void partListing(String key){
|
|
|
+ String uploadId = "490C8D60323F4AE3ACA2C627A785902A";
|
|
|
+ // 列举已上传的分片,其中uploadId来自于initiateMultipartUpload
|
|
|
+ ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
|
|
|
+ PartListing partListing = ossClient.listParts(listPartsRequest);
|
|
|
+ for (PartSummary part : partListing.getParts()) {
|
|
|
+ // 分片号,上传时候指定
|
|
|
+ part.getPartNumber();
|
|
|
+ // 分片数据大小
|
|
|
+ part.getSize();
|
|
|
+ // Part的ETag
|
|
|
+ part.getETag();
|
|
|
+ // Part的最后修改上传
|
|
|
+ part.getLastModified();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有已上传分片
|
|
|
+ */
|
|
|
+ public ListPartsRequest listParts(String key){
|
|
|
+ String uploadId = getUploadId(key);
|
|
|
+ // 列举所有已上传的分片
|
|
|
+ PartListing partListing;
|
|
|
+ ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
|
|
|
+ do {
|
|
|
+ partListing = ossClient.listParts(listPartsRequest);
|
|
|
+ for (PartSummary part : partListing.getParts()) {
|
|
|
+ // 分片号,上传时候指定
|
|
|
+ part.getPartNumber();
|
|
|
+ // 分片数据大小
|
|
|
+ part.getSize();
|
|
|
+ // Part的ETag
|
|
|
+ part.getETag();
|
|
|
+ // Part的最后修改上传
|
|
|
+ part.getLastModified();
|
|
|
+ }
|
|
|
+ listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
|
|
|
+ } while (partListing.isTruncated());
|
|
|
+ return listPartsRequest;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页获取所有分片
|
|
|
+ */
|
|
|
+ public void listPartsPage(String key){
|
|
|
+ String uploadId = getUploadId(key);
|
|
|
+ // 分页列举已上传的分片
|
|
|
+ PartListing partListing;
|
|
|
+ ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
|
|
|
+ // 每页100个分片
|
|
|
+ listPartsRequest.setMaxParts(100);
|
|
|
+ do {
|
|
|
+ partListing = ossClient.listParts(listPartsRequest);
|
|
|
+ for (PartSummary part : partListing.getParts()) {
|
|
|
+ // 分片号,上传时候指定
|
|
|
+ part.getPartNumber();
|
|
|
+ // 分片数据大小
|
|
|
+ part.getSize();
|
|
|
+ // Part的ETag
|
|
|
+ part.getETag();
|
|
|
+ // Part的最后修改上传
|
|
|
+ part.getLastModified();
|
|
|
+ }
|
|
|
+ listPartsRequest.setPartNumberMarker(partListing.getNextPartNumberMarker());
|
|
|
+ } while (partListing.isTruncated());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Description: 判断OSS服务文件上传时文件的contentType
|
|
|
+ *
|
|
|
+ * @param FilenameExtension 文件后缀
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getcontentType(String FilenameExtension) {
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("bmp")) {
|
|
|
+ return "image/bmp";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("gif")) {
|
|
|
+ return "image/gif";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("jpeg") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase("jpg") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase("png")) {
|
|
|
+ return "image/jpeg";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("html")) {
|
|
|
+ return "text/html";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("txt")) {
|
|
|
+ return "text/plain";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("vsd")) {
|
|
|
+ return "application/vnd.visio";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("pptx") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase("ppt")) {
|
|
|
+ return "application/vnd.ms-powerpoint";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("docx") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase("doc")) {
|
|
|
+ return "application/msword";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase("xml")) {
|
|
|
+ return "text/xml";
|
|
|
+ }
|
|
|
+ return "image/jpeg";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得url链接
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getUrl(String key) {
|
|
|
+ // 设置URL过期时间为10年 3600l* 1000*24*365*10
|
|
|
+ Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
|
|
|
+ // 生成URL
|
|
|
+ URL url = ossClient.generatePresignedUrl(bucketName, key, expiration);
|
|
|
+ if (url != null) {
|
|
|
+ return url.toString();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMd5ByFile(File file) throws FileNotFoundException {
|
|
|
+ String value = null;
|
|
|
+ FileInputStream in = new FileInputStream(file);
|
|
|
+ try {
|
|
|
+ MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
|
|
|
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
+ md5.update(byteBuffer);
|
|
|
+ BigInteger bi = new BigInteger(1, md5.digest());
|
|
|
+ value = bi.toString(16);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if(null != in) {
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除单个文件
|
|
|
+ * @param key Bucket下的文件的路径名+文件名
|
|
|
+ */
|
|
|
+ public void deleteSingleObject(String key){
|
|
|
+ OSSObject object = null ;
|
|
|
+ try {
|
|
|
+ object = ossClient.getObject(bucketName, key);
|
|
|
+ if(object != null){
|
|
|
+ ossClient.deleteObject(bucketName, key);
|
|
|
+ }
|
|
|
+ } catch (OSSException oe) {
|
|
|
+ oe.printStackTrace();
|
|
|
+ } catch (ClientException ce) {
|
|
|
+ ce.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if(ossClient != null){
|
|
|
+ try {
|
|
|
+ ossClient.shutdown();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void downLoadFile(String key,String fileName) {
|
|
|
+ String bucketName = Global.getBucketName();
|
|
|
+ try {
|
|
|
+ // 带进度条的下载
|
|
|
+ ossClient.getObject(new GetObjectRequest(bucketName, key).
|
|
|
+ <GetObjectRequest>withProgressListener(new GetObjectProgressListener()),
|
|
|
+ new File(fileName));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ossClient.shutdown();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 附件下载
|
|
|
+ * @param key
|
|
|
+ * @param fileName
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ public void downByStream(String key, String fileName, HttpServletResponse response,String agent){
|
|
|
+ try {
|
|
|
+ String newName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20").replaceAll("%28", "\\(").replaceAll("%29", "\\)").replaceAll("%3B", ";").replaceAll("%40", "@").replaceAll("%23", "\\#").replaceAll("%26", "\\&").replaceAll("%2C", "\\,");
|
|
|
+ // 创建OSSClient实例
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ newName = URLEncoder.encode(fileName.substring(13, fileName.length()),"UTF8");
|
|
|
+ response.setHeader("Content-Disposition","attachment;filename*=UTF-8''"+ newName);
|
|
|
+
|
|
|
+ /*if(agent != null && agent.toLowerCase().indexOf("firefox") > 0){
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''"+ URLEncoder.encode(fileName,"utf-8"));
|
|
|
+ }else {
|
|
|
+ response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
|
|
|
+ }*/
|
|
|
+ byte[] car=new byte[1024];
|
|
|
+ int L=0;
|
|
|
+ while((L=in.read(car))!=-1){
|
|
|
+ out.write(car, 0,L);
|
|
|
+ }
|
|
|
+ if(out!=null){
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 附件下载到本地指定文件夹
|
|
|
+ * @param key
|
|
|
+ * @param fileName
|
|
|
+ */
|
|
|
+ public void downByStreamSaveLocal(String key, String fileName,String downFileStr){
|
|
|
+ try {
|
|
|
+ // 创建OSSClient实例
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+
|
|
|
+
|
|
|
+ //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
|
|
|
+ FileOutputStream fileOut = new FileOutputStream(downFileStr);
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(fileOut);
|
|
|
+ byte[] buf = new byte[4096];
|
|
|
+ int length = in.read(buf);
|
|
|
+ //保存文件
|
|
|
+ while(length != -1)
|
|
|
+ {
|
|
|
+ bos.write(buf, 0, length);
|
|
|
+ length = in.read(buf);
|
|
|
+ }
|
|
|
+ if(bos!=null){
|
|
|
+ bos.flush();
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件下载
|
|
|
+ * @param key
|
|
|
+ * @param fileName
|
|
|
+ */
|
|
|
+ public byte[] downBytesByStream(String key, String fileName){
|
|
|
+ byte[] bytes = null;
|
|
|
+ BufferedInputStream in = null;
|
|
|
+ ByteArrayOutputStream outputStream = null;
|
|
|
+ logger.info("开始从云服务器加载文件,文件名:{}",fileName);
|
|
|
+ try {
|
|
|
+ // 创建OSSClient实例
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ outputStream = new ByteArrayOutputStream();
|
|
|
+ byte[] car=new byte[1024];
|
|
|
+ int L=0;
|
|
|
+ while((L=in.read(car))!=-1){
|
|
|
+ outputStream.write(car, 0,L);
|
|
|
+ }
|
|
|
+ bytes = outputStream.toByteArray();
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
+ logger.info("从云服务器加载文件成功,文件名:{},耗时:{}ms",fileName,end-start);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if (in!=null){
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (outputStream!=null){
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 附件转pdf
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ public BufferedInputStream recInputStream(String key){
|
|
|
+ // 创建OSSClient实例
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ return in;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args){
|
|
|
+ /* OSSClientUtil os = new OSSClientUtil();
|
|
|
+ os.downByOpenOffice("app-data/notify/35690003238131.docx","e:/pre.pdf","");*/
|
|
|
+ /*String endpoint = Global.getEndpoint();
|
|
|
+ String accessKeyId = Global.getAccessKeyId();
|
|
|
+ String accessKeySecret = Global.getAccessKeySecret();
|
|
|
+ String bucketName = Global.getBucketName();
|
|
|
+ OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ String key = "app-data/appfiles/932059357.war";
|
|
|
+ String url = "F:\\code\\jeeplus_maven\\target\\jeeplus.war";
|
|
|
+ // 设置断点续传请求
|
|
|
+ UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
|
|
|
+ // 指定上传的本地文件
|
|
|
+ uploadFileRequest.setUploadFile(url);
|
|
|
+ // 指定上传并发线程数
|
|
|
+ uploadFileRequest.setTaskNum(5);
|
|
|
+ // 指定上传的分片大小
|
|
|
+ uploadFileRequest.setPartSize(1 * 1024 * 1024);
|
|
|
+ // 开启断点续传
|
|
|
+ uploadFileRequest.setEnableCheckpoint(true);
|
|
|
+ // 断点续传上传
|
|
|
+ try {
|
|
|
+ ossClient.uploadFile(uploadFileRequest);
|
|
|
+ } catch (Throwable throwable) {
|
|
|
+ throwable.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 带进度条的上传
|
|
|
+ ossClient.putObject(new PutObjectRequest(bucketName, key, new FileInputStream(url)).
|
|
|
+ <PutObjectRequest>withProgressListener(new PutObjectProgressListener()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ ossClient.shutdown();*/
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阿里云获取临时文件查看url
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public String getFileTemporaryLookUrl(String file){
|
|
|
+ URL url = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ file = file.replace("amp;","");
|
|
|
+ String aliyunUrl = Global.getAliyunUrl();
|
|
|
+ String aliDownloadUrl = Global.getAliDownloadUrl();
|
|
|
+ String cons = "";
|
|
|
+ if (file.contains(aliyunUrl)){
|
|
|
+ cons = aliyunUrl;
|
|
|
+ }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
|
|
|
+ cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ }else {
|
|
|
+ cons = aliDownloadUrl;
|
|
|
+ }
|
|
|
+ String key = file.split(cons+"/")[1];
|
|
|
+ // 指定过期时间为24小时。
|
|
|
+ Date expiration = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 );
|
|
|
+ url = ossClient.generatePresignedUrl(bucketName, key, expiration);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return url.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阿里云获取临时文件查看url
|
|
|
+ * 签章文件查看
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public String getQzFileTemporaryLookUrl(String file){
|
|
|
+ URL url = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ file = file.replace("amp;","");
|
|
|
+ String aliyunUrl = Global.getAliyunUrl();
|
|
|
+ String aliDownloadUrl = Global.getAliDownloadUrl();
|
|
|
+ String cons = "";
|
|
|
+ if (file.contains(aliyunUrl)){
|
|
|
+ cons = aliyunUrl;
|
|
|
+ }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
|
|
|
+ cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ }else {
|
|
|
+ cons = aliDownloadUrl;
|
|
|
+ }
|
|
|
+ String key = file.split(cons+"/")[1];
|
|
|
+ // 指定过期时间为24小时。
|
|
|
+ Date expiration = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 );
|
|
|
+ url = ossClient.generatePresignedUrl(qzBucketName, key, expiration);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return url.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阿里云获取临时文件大小
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public Long getSimplifiedObjectMeta(String file){
|
|
|
+ URL url = null;
|
|
|
+ SimplifiedObjectMeta simplifiedObjectMeta = new SimplifiedObjectMeta();
|
|
|
+ try {
|
|
|
+
|
|
|
+ file = file.replace("amp;","");
|
|
|
+ String aliyunUrl = Global.getAliyunUrl();
|
|
|
+ String aliDownloadUrl = Global.getAliDownloadUrl();
|
|
|
+ String cons = "";
|
|
|
+ if (file.contains(aliyunUrl)){
|
|
|
+ cons = aliyunUrl;
|
|
|
+ }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
|
|
|
+ cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ }else {
|
|
|
+ cons = aliDownloadUrl;
|
|
|
+ }
|
|
|
+ String key = file.split(cons+"/")[1];
|
|
|
+ simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
|
|
|
+ System.out.println(simplifiedObjectMeta.getSize());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return simplifiedObjectMeta.getSize();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阿里云获取临时文件大小
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public Long getQzSimplifiedObjectMeta(String file){
|
|
|
+ URL url = null;
|
|
|
+ SimplifiedObjectMeta simplifiedObjectMeta = new SimplifiedObjectMeta();
|
|
|
+ try {
|
|
|
+
|
|
|
+ file = file.replace("amp;","");
|
|
|
+ String aliyunUrl = Global.getAliyunUrl();
|
|
|
+ String aliDownloadUrl = Global.getAliDownloadUrl();
|
|
|
+ String cons = "";
|
|
|
+ if (file.contains(aliyunUrl)){
|
|
|
+ cons = aliyunUrl;
|
|
|
+ }else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
|
|
|
+ cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
|
|
|
+ }else {
|
|
|
+ cons = aliDownloadUrl;
|
|
|
+ }
|
|
|
+ String key = file.split(cons+"/")[1];
|
|
|
+ simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(qzBucketName, key);
|
|
|
+ System.out.println(simplifiedObjectMeta.getSize());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return simplifiedObjectMeta.getSize();
|
|
|
+ }
|
|
|
+}
|