|
|
@@ -1,460 +1,465 @@
|
|
|
-package org.jeecg.common.oss;
|
|
|
-
|
|
|
-import com.aliyun.oss.ClientException;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.aliyun.oss.OSSException;
|
|
|
-import com.aliyun.oss.model.*;
|
|
|
-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 String endpoint = OSSConfig.JAVA4ALL_END_POINT;
|
|
|
- private String accessKeyId = OSSConfig.JAVA4ALL_ACCESS_KEY_ID;
|
|
|
- private String accessKeySecret = OSSConfig.JAVA4ALL_ACCESS_KEY_SECRET;
|
|
|
- private String bucketName = OSSConfig.JAVA4ALL_BUCKET_NAME;
|
|
|
- private String savePath = OSSConfig.JAVA4ALL_SAVE_PATH;
|
|
|
- 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();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public String uploadImg2OSS(MultipartFile file, String fileDir) {
|
|
|
- if (file.getSize() > 100 * 1024 * 1024) {
|
|
|
- System.out.println("上传文件大小不能超过100M!");
|
|
|
- return "上传文件大小不能超过100M!";
|
|
|
- }
|
|
|
- //文件名
|
|
|
- String originalFilename = file.getOriginalFilename();
|
|
|
- //获取文件后缀
|
|
|
- String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
- //新文件名
|
|
|
- String fileName = "";
|
|
|
- fileDir = fileDir + System.nanoTime() + "/";
|
|
|
- try {
|
|
|
- //文件输入流
|
|
|
- InputStream inputStream = file.getInputStream();
|
|
|
- File f = File.createTempFile("tmp", null);
|
|
|
- file.transferTo(f);
|
|
|
- f.deleteOnExit();
|
|
|
- String md5 = getMd5ByFile(f);
|
|
|
- //新文件名=MD5加密+后缀名
|
|
|
- fileName = md5 + substring;
|
|
|
- this.uploadFile2OSS(inputStream, fileDir, fileName);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (Exception e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- System.out.println("文件上传失败");
|
|
|
- }
|
|
|
- String name = "/" + fileDir + fileName;
|
|
|
- return name;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传到OSS服务器 如果同名文件会覆盖服务器上的
|
|
|
- *
|
|
|
- * @param fileName 文件名称 包括后缀名
|
|
|
- * @return 出错返回"" ,唯一MD5数字签名
|
|
|
- */
|
|
|
- public String uploadFile2OSS(InputStream inStream, String fileDir, String fileName) {
|
|
|
- //上传文件
|
|
|
- ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
- objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
|
|
|
- PutObjectResult putResult = ossClient.putObject(bucketName, fileDir + fileName, inStream, objectMetadata);
|
|
|
- String ret = putResult.getETag();
|
|
|
- try {
|
|
|
- if (inStream != null) {
|
|
|
- inStream.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
- 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/jpg";
|
|
|
- }
|
|
|
- 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";
|
|
|
- }
|
|
|
- if (FilenameExtension.equalsIgnoreCase(".pdf")) {
|
|
|
- return "application/pdf";
|
|
|
- }
|
|
|
- if (FilenameExtension.equalsIgnoreCase(".xla") ||
|
|
|
- FilenameExtension.equalsIgnoreCase(".xlc") ||
|
|
|
- FilenameExtension.equalsIgnoreCase(".xlm") ||
|
|
|
- FilenameExtension.equalsIgnoreCase(".xls") ||
|
|
|
- FilenameExtension.equalsIgnoreCase(".xlt") ||
|
|
|
- FilenameExtension.equalsIgnoreCase(".xlw")) {
|
|
|
- return "application/vnd.ms-excel";
|
|
|
- }
|
|
|
- return "image/jpg";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获得url链接
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String getUrl(String key) {
|
|
|
- // 设置URL过期时间为10年 3600l* 1000*24*365*10
|
|
|
- Date expiration = new Date(System.currentTimeMillis() + 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();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 单个下载文件
|
|
|
- *
|
|
|
- * @param key
|
|
|
- * @param fileName
|
|
|
- */
|
|
|
- public void downLoadFile(String key, String fileName) {
|
|
|
- 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) {
|
|
|
- try {
|
|
|
- // 创建OSSClient实例
|
|
|
- OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
- BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
- BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
- 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();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 附件转pdf
|
|
|
- *
|
|
|
- * @param key
|
|
|
- */
|
|
|
- public BufferedInputStream recInputStream(String key) {
|
|
|
- // 创建OSSClient实例
|
|
|
- OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
- BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
- return in;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package org.jeecg.common.oss;
|
|
|
+
|
|
|
+import com.aliyun.oss.ClientException;
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import com.aliyun.oss.OSSException;
|
|
|
+import com.aliyun.oss.model.*;
|
|
|
+import org.jeecg.common.util.StringUtils;
|
|
|
+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 String endpoint = OSSConfig.JAVA4ALL_END_POINT;
|
|
|
+ private String accessKeyId = OSSConfig.JAVA4ALL_ACCESS_KEY_ID;
|
|
|
+ private String accessKeySecret = OSSConfig.JAVA4ALL_ACCESS_KEY_SECRET;
|
|
|
+ private String bucketName = OSSConfig.JAVA4ALL_BUCKET_NAME;
|
|
|
+ private String savePath = OSSConfig.JAVA4ALL_SAVE_PATH;
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String uploadImg2OSS(MultipartFile file, String fileDir) {
|
|
|
+ if (file.getSize() > 100 * 1024 * 1024) {
|
|
|
+ System.out.println("上传文件大小不能超过100M!");
|
|
|
+ return "上传文件大小不能超过100M!";
|
|
|
+ }
|
|
|
+ //文件名
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ //获取文件后缀
|
|
|
+ String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
|
|
|
+ //新文件名
|
|
|
+ String fileName = "";
|
|
|
+ fileDir = fileDir + System.nanoTime() + "/";
|
|
|
+ try {
|
|
|
+ //文件输入流
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ File f = File.createTempFile("tmp", null);
|
|
|
+ file.transferTo(f);
|
|
|
+ f.deleteOnExit();
|
|
|
+ String md5 = getMd5ByFile(f);
|
|
|
+ //新文件名=MD5加密+后缀名
|
|
|
+ fileName = md5 + substring;
|
|
|
+ //上传返回
|
|
|
+ String ret = this.uploadFile2OSS(inputStream, fileDir, fileName);
|
|
|
+ if (StringUtils.isBlank(ret)) {
|
|
|
+ return "文件上传失败";
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ System.out.println("文件上传失败");
|
|
|
+ }
|
|
|
+ String name = "/" + fileDir + fileName;
|
|
|
+ return name;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传到OSS服务器 如果同名文件会覆盖服务器上的
|
|
|
+ *
|
|
|
+ * @param fileName 文件名称 包括后缀名
|
|
|
+ * @return 出错返回"" ,唯一MD5数字签名
|
|
|
+ */
|
|
|
+ public String uploadFile2OSS(InputStream inStream, String fileDir, String fileName) {
|
|
|
+ //上传文件
|
|
|
+ ObjectMetadata objectMetadata = new ObjectMetadata();
|
|
|
+ objectMetadata.setContentType(getcontentType(fileName.substring(fileName.lastIndexOf("."))));
|
|
|
+ PutObjectResult putResult = ossClient.putObject(bucketName, fileDir + fileName, inStream, objectMetadata);
|
|
|
+ String ret = putResult.getETag();
|
|
|
+ try {
|
|
|
+ if (inStream != null) {
|
|
|
+ inStream.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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/jpg";
|
|
|
+ }
|
|
|
+ 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";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase(".pdf")) {
|
|
|
+ return "application/pdf";
|
|
|
+ }
|
|
|
+ if (FilenameExtension.equalsIgnoreCase(".xla") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase(".xlc") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase(".xlm") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase(".xls") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase(".xlt") ||
|
|
|
+ FilenameExtension.equalsIgnoreCase(".xlw")) {
|
|
|
+ return "application/vnd.ms-excel";
|
|
|
+ }
|
|
|
+ return "image/jpg";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得url链接
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getUrl(String key) {
|
|
|
+ // 设置URL过期时间为10年 3600l* 1000*24*365*10
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 单个下载文件
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @param fileName
|
|
|
+ */
|
|
|
+ public void downLoadFile(String key, String fileName) {
|
|
|
+ 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) {
|
|
|
+ try {
|
|
|
+ // 创建OSSClient实例
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 附件转pdf
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ public BufferedInputStream recInputStream(String key) {
|
|
|
+ // 创建OSSClient实例
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, key);
|
|
|
+ BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
|
+ return in;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|