|
@@ -0,0 +1,525 @@
|
|
|
|
+package com.fdkankan.file.oss;
|
|
|
|
+
|
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
|
+import com.aliyun.oss.model.OSSObjectSummary;
|
|
|
|
+import com.aliyun.oss.model.ObjectListing;
|
|
|
|
+import com.aliyun.oss.model.ObjectMetadata;
|
|
|
|
+import com.aliyun.oss.model.PutObjectResult;
|
|
|
|
+import com.amazonaws.HttpMethod;
|
|
|
|
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
|
|
|
+import com.amazonaws.auth.BasicAWSCredentials;
|
|
|
|
+import com.amazonaws.regions.Regions;
|
|
|
|
+import com.amazonaws.services.s3.AmazonS3;
|
|
|
|
+import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
|
|
|
+import com.amazonaws.services.s3.model.CannedAccessControlList;
|
|
|
|
+import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
|
|
|
|
+import com.amazonaws.services.s3.model.GetObjectRequest;
|
|
|
|
+import com.amazonaws.services.s3.model.PutObjectRequest;
|
|
|
|
+import com.qiniu.common.Zone;
|
|
|
|
+import com.qiniu.storage.Configuration;
|
|
|
|
+import com.qiniu.storage.UploadManager;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
|
+import org.apache.commons.fileupload.FileItemFactory;
|
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
+
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.net.FileNameMap;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Component
|
|
|
|
+public class UploadToOssUtil {
|
|
|
|
+
|
|
|
|
+ Zone zone = Zone.autoZone();
|
|
|
|
+
|
|
|
|
+ Configuration config = new Configuration(zone);
|
|
|
|
+ UploadManager uploadManager = new UploadManager(config);
|
|
|
|
+
|
|
|
|
+ @Value("${oss.point:http://oss-cn-shenzhen-internal.aliyuncs.com}")
|
|
|
|
+ private String point;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.key:LTAIUrvuHqj8pvry}")
|
|
|
|
+ private String key;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.secrey:JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4}")
|
|
|
|
+ private String secrey;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.bucket:4dkankan}")
|
|
|
|
+ private String bucket;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.sdk:4dscene}")
|
|
|
|
+ private String bucketSdk;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Value("${oss.type:oss}")
|
|
|
|
+ private String type;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.s3key:AKIAWCV5QFZ3ZNELKYUY}")
|
|
|
|
+ private String s3key;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.s3secrey:epS5ghyR4LJ7rxk/qJO9ZYh6m9Oz6g5haKDu4yws}")
|
|
|
|
+ private String s3secrey;
|
|
|
|
+
|
|
|
|
+ @Value("${oss.s3bucket:4dkankan}")
|
|
|
|
+ private String s3bucket;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void delete(String key1) throws IOException{
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ try {
|
|
|
|
+ // bucketMgr.delete(bucketname, key);
|
|
|
|
+
|
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
|
+ ossClient.deleteObject(bucket, key1);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //上传的数据是byte[],key是上传后的文件名
|
|
|
|
+ public void upload(byte[] data,String key1) throws IOException{
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ //bucketMgr.delete(bucketname, key);
|
|
|
|
+ //Response res = uploadManager.put(data, key, getUpToken(key));
|
|
|
|
+
|
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
|
+ ossClient.putObject(bucket, key1, new ByteArrayInputStream(data));
|
|
|
|
+ //log.info(res.bodyString());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.toString()+key1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public void upload(String filePath, String key1) {
|
|
|
|
+ log.info("开始上传文件 源路径:{},目标路径:{},point:{}" , filePath,key1,point);
|
|
|
|
+ if("oss".equals(type)){
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ try {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
|
+ }
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
|
+ }
|
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if("s3".equals(type)){
|
|
|
|
+ try{
|
|
|
|
+ uploadS3File(filePath, key1);
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void uploadSdk(String filePath, String key1) {
|
|
|
|
+ if("oss".equals(type)){
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ try {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
|
+ }
|
|
|
|
+ // 调用put方法上传
|
|
|
|
+ // Response res = uploadManager.put(FilePath, key, getUpToken(key));
|
|
|
|
+ // 打印返回的信息
|
|
|
|
+ // log.info(res.bodyString());
|
|
|
|
+
|
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
|
+// ObjectMetadata meta = new ObjectMetadata();
|
|
|
|
+// meta.setCacheControl("no-cache");
|
|
|
|
+// ossClient.putObject(BUCKET_NAME, key, new File(filePath), meta);
|
|
|
|
+
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
|
+ }
|
|
|
|
+ ossClient.putObject(bucketSdk, key1, new File(filePath), metadata);
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if("s3".equals(type)){
|
|
|
|
+ try{
|
|
|
|
+ uploadS3File(filePath, key1);
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void upload2(String filePath, String key1) {
|
|
|
|
+ log.info("开始上传文件 源路径:{},目标路径:{},point:{}" , filePath,key1,point);
|
|
|
|
+ if("oss".equals(type)){
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ try {
|
|
|
|
+ // 调用put方法上传
|
|
|
|
+ // Response res = uploadManager.put(FilePath, key, getUpToken(key));
|
|
|
|
+ // 打印返回的信息
|
|
|
|
+ // log.info(res.bodyString());
|
|
|
|
+
|
|
|
|
+ // 2019-2-28 启动aliyun oss 空间
|
|
|
|
+ ObjectMetadata metadata = new ObjectMetadata();
|
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
|
+ }
|
|
|
|
+ if(filePath.contains(".mp4")){
|
|
|
|
+ metadata.setContentType("video/mp4");
|
|
|
|
+ }
|
|
|
|
+ if(filePath.contains(".mp3")){
|
|
|
|
+ metadata.setContentType("audio/mp3");
|
|
|
|
+ }
|
|
|
|
+ ossClient.putObject(bucket, key1, new File(filePath), metadata);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if("s3".equals(type)){
|
|
|
|
+ try{
|
|
|
|
+ uploadS3File(filePath, key1);
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
|
|
|
|
+ public void uploadMulFiles(Map<String, String> filepaths) {
|
|
|
|
+ if (filepaths == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ Long start = System.currentTimeMillis();
|
|
|
|
+ log.info("开始批量上传文件:");
|
|
|
|
+ if (filepaths.size() > 50) {
|
|
|
|
+ filepaths.entrySet().parallelStream().forEach(entry->{
|
|
|
|
+ upload2(entry.getKey(), entry.getValue());
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ filepaths.entrySet().parallelStream().forEach(entry->{
|
|
|
|
+ upload(entry.getKey(), entry.getValue());
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ log.info("批量上传文件结束,用时:{}" ,(System.currentTimeMillis() - start));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public int deleteFile(String prefix){
|
|
|
|
+ if("oss".equals(type)){
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+
|
|
|
|
+ ObjectListing objectListing = ossClient.listObjects(bucket, prefix);
|
|
|
|
+ List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
|
|
|
|
+ try {
|
|
|
|
+ for (OSSObjectSummary s : sums) {
|
|
|
|
+ delete(s.getKey());
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if("s3".equals(type)){
|
|
|
|
+ deleteS3Object(prefix);
|
|
|
|
+ }
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public Map<String, String> getUploadS3Url(List<String> urls){
|
|
|
|
+ if(urls == null || urls.size() <= 0){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ BasicAWSCredentials awsCred = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
|
+
|
|
|
|
+ AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(awsCred))
|
|
|
|
+ .withRegion(Regions.EU_WEST_2)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ // Set the pre-signed URL to expire after one hour.
|
|
|
|
+ java.util.Date expiration = new java.util.Date();
|
|
|
|
+ long expTimeMillis = expiration.getTime();
|
|
|
|
+ expTimeMillis += 1000 * 60 * 60 * 8;
|
|
|
|
+ expiration.setTime(expTimeMillis);
|
|
|
|
+
|
|
|
|
+ //生成预签名URL
|
|
|
|
+ log.info("生成预签名URL");
|
|
|
|
+ GeneratePresignedUrlRequest generatePresignedUrlRequest = null;
|
|
|
|
+ URL url = null;
|
|
|
|
+ Map<String, String> map = new HashMap();
|
|
|
|
+ for(String path : urls){
|
|
|
|
+// if(path.contains(".jpg") || path.contains("png")){
|
|
|
|
+// generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path)
|
|
|
|
+// .withMethod(HttpMethod.PUT)
|
|
|
|
+// .withExpiration(expiration)
|
|
|
|
+// .withContentType("image/jpeg");
|
|
|
|
+// }else {
|
|
|
|
+ generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, path)
|
|
|
|
+ .withMethod(HttpMethod.PUT)
|
|
|
|
+ .withExpiration(expiration);
|
|
|
|
+// }
|
|
|
|
+ url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
|
|
|
|
+ map.put(path, url.toString());
|
|
|
|
+ }
|
|
|
|
+ return map;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String upload5(String filePath, String key1) {
|
|
|
|
+ OSSClient ossClient = new OSSClient(point, key, secrey);
|
|
|
|
+ PutObjectResult result = null;
|
|
|
|
+ try {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ log.error("要上传的文件不存在:" + filePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ result = ossClient.putObject(bucket, key1, new File(filePath));
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(e.toString() + filePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ log.info(" getETag : " + result.getETag());
|
|
|
|
+ log.info("1 : " + result.toString());
|
|
|
|
+ log.info("2 : " + result.getRequestId());
|
|
|
|
+ log.info("3 : " + result.getClientCRC());
|
|
|
|
+ log.info("4 : " + result.getResponse());
|
|
|
|
+ log.info("5 : " + result.getServerCRC());
|
|
|
|
+ return result.getETag();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //海外亚马逊s3
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * s3上传文件流
|
|
|
|
+ *
|
|
|
|
+ * @param file 文件
|
|
|
|
+ * @param updatePath 上传路径[ eg: xxx/xxx ]
|
|
|
|
+ */
|
|
|
|
+ public String updateS3LoadFile(MultipartFile file, String updatePath) {
|
|
|
|
+
|
|
|
|
+ if (isEmpty(file)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建s3对象
|
|
|
|
+ */
|
|
|
|
+ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
|
+ AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
|
|
+ .withRegion(Regions.EU_WEST_2)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 创建临时文件,程序运行结束,会自动删除
|
|
|
|
+ File localFile = File.createTempFile("temp", null);
|
|
|
|
+ // 把文件写入内存中
|
|
|
|
+ file.transferTo(localFile);
|
|
|
|
+
|
|
|
|
+ // 指定要上传到服务器上的路径
|
|
|
|
+ String key = updatePath;
|
|
|
|
+
|
|
|
|
+ // 设置文件并设置公读
|
|
|
|
+ PutObjectRequest request = new PutObjectRequest(s3bucket, key, localFile);
|
|
|
|
+ request.withCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
|
+
|
|
|
|
+ // 上传文件
|
|
|
|
+ com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request);
|
|
|
|
+ if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
|
|
|
|
+ System.out.println("success");
|
|
|
|
+ return key;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * s3上传文件
|
|
|
|
+ * @param filePath
|
|
|
|
+ * @param key1
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ private void uploadS3File(String filePath, String key1) throws IOException {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ if(!file.exists()){
|
|
|
|
+ log.info("要上传s3的文件不存在");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建s3对象
|
|
|
|
+ */
|
|
|
|
+ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
|
+ AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
|
|
+ .withRegion(Regions.EU_WEST_2)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ // 设置文件并设置公读
|
|
|
|
+ com.amazonaws.services.s3.model.ObjectMetadata metadata = new com.amazonaws.services.s3.model.ObjectMetadata();
|
|
|
|
+ if(filePath.contains(".jpg")){
|
|
|
|
+ metadata.setContentType("image/jpeg");
|
|
|
|
+ }
|
|
|
|
+ if(filePath.contains(".png")){
|
|
|
|
+ metadata.setContentType("image/png");
|
|
|
|
+ }
|
|
|
|
+ PutObjectRequest request = new PutObjectRequest(s3bucket, key1, file);
|
|
|
|
+ request.withCannedAcl(CannedAccessControlList.PublicRead);
|
|
|
|
+ request.withMetadata(metadata);
|
|
|
|
+
|
|
|
|
+ // 上传文件
|
|
|
|
+ com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(request);
|
|
|
|
+ if (StringUtils.isNotEmpty(putObjectResult.getETag())) {
|
|
|
|
+ log.info("s3上传文件成功:" + key1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除单个文件
|
|
|
|
+ *
|
|
|
|
+ * @param filePath 文件路径[ eg: /head/xxxx.jpg ]
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public void deleteS3Object(String filePath) {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建s3对象
|
|
|
|
+ */
|
|
|
|
+ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
|
+ AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
|
|
+ .withRegion(Regions.EU_WEST_2)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ if (filePath.startsWith("/")) {
|
|
|
|
+ filePath = filePath.substring(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ s3.deleteObject(s3bucket, filePath);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取文件类型
|
|
|
|
+ */
|
|
|
|
+ public static String getContentType(String filePath){
|
|
|
|
+ FileNameMap fileNameMap = URLConnection.getFileNameMap();
|
|
|
|
+ String contentType = fileNameMap.getContentTypeFor(filePath);
|
|
|
|
+ System.out.println(contentType);
|
|
|
|
+ return contentType;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 检查文件是否为空
|
|
|
|
+ *
|
|
|
|
+ * @param imageFile
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private static boolean isEmpty(MultipartFile imageFile) {
|
|
|
|
+ if (imageFile == null || imageFile.getSize() <= 0) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static MultipartFile getMulFileByPath(String picPath) {
|
|
|
|
+ FileItem fileItem = createFileItem(picPath);
|
|
|
|
+ MultipartFile mfile = new CommonsMultipartFile(fileItem);
|
|
|
|
+ return mfile;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static FileItem createFileItem(String filePath)
|
|
|
|
+ {
|
|
|
|
+ FileItemFactory factory = new DiskFileItemFactory(16, null);
|
|
|
|
+ String textFieldName = "textField";
|
|
|
|
+ int num = filePath.lastIndexOf(".");
|
|
|
|
+ String extFile = filePath.substring(num);
|
|
|
|
+ FileItem item = factory.createItem(textFieldName, "text/plain", true,
|
|
|
|
+ "MyFileName" + extFile);
|
|
|
|
+ File newfile = new File(filePath);
|
|
|
|
+ int bytesRead = 0;
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ FileInputStream fis = new FileInputStream(newfile);
|
|
|
|
+ OutputStream os = item.getOutputStream();
|
|
|
|
+ while ((bytesRead = fis.read(buffer, 0, 8192))
|
|
|
|
+ != -1)
|
|
|
|
+ {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ os.close();
|
|
|
|
+ fis.close();
|
|
|
|
+ }
|
|
|
|
+ catch (IOException e)
|
|
|
|
+ {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return item;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载文件
|
|
|
|
+ * @param bucketName 桶名
|
|
|
|
+ * @param remoteFileName 文件名
|
|
|
|
+ * @param path 下载路径
|
|
|
|
+ */
|
|
|
|
+ public boolean downFromS3(String bucketName, String remoteFileName, String path) {
|
|
|
|
+ try {
|
|
|
|
+ /**
|
|
|
|
+ * 创建s3对象
|
|
|
|
+ */
|
|
|
|
+ BasicAWSCredentials awsCreds = new BasicAWSCredentials(s3key, s3secrey);
|
|
|
|
+ AmazonS3 s3 = AmazonS3ClientBuilder.standard()
|
|
|
|
+ .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
|
|
|
|
+ .withRegion(Regions.EU_WEST_2)
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ GetObjectRequest request = new GetObjectRequest(bucketName,remoteFileName);
|
|
|
|
+ s3.getObject(request,new File(path));
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception ase) {
|
|
|
|
+ log.error("amazonS3下载文件异常 " + ase.getMessage(), ase);
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|