package com.fdkankan.common.util;

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 {

//    String ACCESS_KEY = "dlPPwgZky_F-iP8CbSbJpiAtAcqw3BYwb9rdHMrS";
//    String SECRET_KEY = "YEtkLKDsImXB-8m1CT1zV_YwCwwGvrUvo2ktj9KZ";
//
//    String bucketname = "scene3d";

//    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

	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.bucket:4dkankan}")
	private String bucket;

	@Value("${oss.bucket.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("开始上传文件 源路径：{},目标路径：{}" , filePath,key1);
		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("开始上传文件 源路径：{},目标路径：{}" , filePath,key1);
		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 static void main(String[] args) throws IOException {

//		UploadToQiniuForScene3DServiceImpl impl = new UploadToQiniuForScene3DServiceImpl();
//		impl.delete("read_me.txt");

//		impl.upload("C:\\Users\\Administrator\\Desktop\\update\\read_me.txt", "read_me.txt");
//		Map<String, String> filepaths = new HashMap<>();
//		filepaths.put("C:\\Users\\Administrator\\Desktop\\update\\read_me.txt", "txt/read_me1.txt");
//		filepaths.put("C:\\Users\\Administrator\\Desktop\\update\\read_me2.txt", "txt/read_me2.txt");
//		impl.uploadMulFiles(filepaths);

//		impl.deleteFile("txt");


//		byte[] content = "Hello OSS".getBytes();
//		impl.upload(content, "Hello OSS");
//		UploadToOssUtil uploadToOssUtil = new UploadToOssUtil();
//		uploadToOssUtil.downFromS3(s3bucket, "log/4DKKPRO_74EE2AB80D9F_2020-04-23.zip", "F:\\桌面\\datagu6HmTLKp\\4DKKPRO_74EE2AB80D9F_2020-04-23.zip");

//		System.out.println("/00152212.jpg".length());

//		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;
//		expiration.setTime(expTimeMillis);
//
//		long begin = System.currentTimeMillis();
//		// Generate the pre-signed URL.
//		System.out.println("Generating pre-signed URL.");
//		GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(s3bucket, "4K-Chimei-inn-2011-1013_30p.mp4")
//				.withMethod(HttpMethod.PUT)
//				.withExpiration(expiration);
//		URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
//		long end = System.currentTimeMillis();
//		System.out.println(end-begin);
//		System.out.println("url:" + url);

		// Create the connection and use it to upload the new object using the pre-signed URL.
//		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//		connection.setDoOutput(true);
//		connection.setRequestMethod("PUT");
//		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
//		out.write("This text uploaded as an object via presigned URL.");
//		out.close();
//
//		// Check the HTTP response code. To complete the upload and make the object available,
//		// you must interact with the connection object in some way.
//		connection.getResponseCode();
//		System.out.println("HTTP response code: " + connection.getResponseCode());
//
//		// Check to make sure that the object was uploaded successfully.
//		S3Object object = s3Client.getObject(bucketName, objectKey);
//		System.out.println("Object " + object.getKey() + " created in bucket " + object.getBucketName());

//		BasicAWSCredentials awsCred = new BasicAWSCredentials("AKIAWCV5QFZ32ZUXFGDR", "WHKH1PxQFdqvRahZpAAdUFvqZpgfJaJXo754vQfM");
//
//		AWSSecurityTokenService sts_client = AWSSecurityTokenServiceClientBuilder.standard()
//				.withCredentials(new AWSStaticCredentialsProvider(awsCred))
//				.withRegion(Regions.EU_WEST_2).build();
//
//		GetFederationTokenRequest getFederationTokenRequest = new GetFederationTokenRequest();
//		getFederationTokenRequest.setDurationSeconds(7200);
//		getFederationTokenRequest.setName("admin");
//
//		// Define the policy and add it to the request.
//		Policy policy = new Policy();
//		policy.withStatements(new Statement(Statement.Effect.Allow)
//				.withActions(S3Actions.ListObjects)
//				.withResources(new Resource("arn:aws:s3:::" + s3bucket)));
//		getFederationTokenRequest.setPolicy(policy.toJson());
//
//		// Get the temporary security credentials.
//		GetFederationTokenResult federationTokenResult = sts_client.getFederationToken(getFederationTokenRequest);
//		Credentials sessionCredentials = federationTokenResult.getCredentials();
//		System.out.println("key:" + sessionCredentials.getAccessKeyId());
//		System.out.println("access:" + sessionCredentials.getSecretAccessKey());
//		System.out.println("token:" + sessionCredentials.getSessionToken());
//
//		BasicSessionCredentials awsCredentials = new BasicSessionCredentials(
//				sessionCredentials.getAccessKeyId(),
//				sessionCredentials.getSecretAccessKey(),
//				sessionCredentials.getSessionToken());
//
//		AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
//				.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
//				.withRegion(Regions.EU_WEST_2)
//				.build();
//		// 设置文件并设置公读
////		PutObjectRequest request = new PutObjectRequest(s3bucket, "log/console2.log", new File("F:\\桌面\\652586408623472640\\60aefd3b380ffc6d5ad3e6aa71b9d817_201912061859305930_edit\\console.log"));
////		request.withCannedAcl(CannedAccessControlList.PublicRead);
////		s3Client.putObject(request);
//		s3Client.putObject(s3bucket, "log/console5.log", new File("F:\\桌面\\652586408623472640\\60aefd3b380ffc6d5ad3e6aa71b9d817_201912061859305930_edit\\console.log"));
//
//		com.amazonaws.services.s3.model.ObjectListing objects = s3Client.listObjects(s3bucket);
//		System.out.println("No. of Objects: " + objects.getObjectSummaries().size());

		try{
//			Map<String,String> map = new HashMap<String,String>();
//			boolean vision2 = false;
//			String projectNum = "eur-enNBY1Trl";
//			String path = "F:\\文档\\710578959925903360\\B58DDC27DB8B4C05B5DBE1AEF1708C5F_202005141944424442";
//			UploadToOssUtil uploadToOssUtil = new UploadToOssUtil();
//			String data = FileUtils.readFile("F:\\文档\\710578959925903360\\B58DDC27DB8B4C05B5DBE1AEF1708C5F_202005141944424442\\results\\upload.json");
//			JSONObject uploadJson = JSON.parseObject(data);
//			JSONArray array = null;
//			array = uploadJson.getJSONArray("upload");
//			if(array == null){
//				throw new Exception("upload.json数据出错");
//			}
//			JSONObject fileJson = null;
//			String fileName = "";
//			String meshfix = "";  //双模型时候会有改文件路径
//			for(int i = 0, len = array.size(); i < len; i++){
//				fileJson = array.getJSONObject(i);
//				fileName = fileJson.getString("file");
//				//文件不存在抛出异常
//				if(!new File(path + File.separator + "results" +File.separator + fileName).exists()){
//					throw new Exception(path + File.separator + "results" +File.separator + fileName+"文件不存在");
//				}
//
//				//判断是否有vision2.txt
//				if("vision2.txt".equals(fileName)){
//					vision2 = true;
//				}
//
//				//tex文件夹
//				if(fileJson.getIntValue("clazz") == 2 && !fileJson.containsKey("pack-file")){
//					if(fileName.contains("meshfix.txt")){
//						meshfix = fileName;
//					}else {
//						map.put(path + File.separator + "results" +File.separator+ fileName,"images/images"+
//								projectNum+"/"+ ConstantFileName.modelUUID+"_50k_texture_jpg_high1/"+fileName.replace("tex/", ""));
//					}
//					continue;
//				}
//
//				//high文件夹
//				if(fileJson.getIntValue("clazz") == 3){
//					map.put(path + File.separator + "results" +File.separator+ fileName,"images/images"+
//							projectNum+"/pan/high/"+ fileName.replace("high/", ""));
//					continue;
//				}
//				//low文件夹
//				if(fileJson.getIntValue("clazz") == 4){
//					map.put(path + File.separator + "results" +File.separator+ fileName,"images/images"+
//							projectNum+"/pan/low/"+ fileName.replace("low/", ""));
//					continue;
//				}
//
//				//tiles文件夹
//				if(fileJson.getIntValue("clazz") == 5 && fileName.contains("4k")){
//					map.put(path + File.separator + "results" + File.separator+ fileName,"images/images"+
//							projectNum+ File.separator + fileName);
//					continue;
//				}
//
//				//updown文件复制一份到ecs中并去掉换行符
//				if(fileJson.getIntValue("clazz") == 10){
//					String updown = FileUtils.readFile(path + File.separator + "results" +File.separator+ fileName);
//					JSONObject updownJson = JSONObject.parseObject(updown);
//					FileUtils.writeFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + projectNum +
//							File.separator + fileName.replace("updown", "mapping"), updownJson.toString());
//					continue;
//				}
//
//				//video视频文件或封面图
////				if(fileJson.getIntValue("clazz") == 11 || fileJson.getIntValue("clazz") == 12){
////					map.put(path + File.separator + "results" + File.separator+ fileName,"video/video"+
////							projectNum+ File.separator + fileName.replace("videos/", ""));
////
////					if(fileName.contains(".mp4")){
////						CreateObjUtil.mp4ToFlv(path + File.separator + "results" + File.separator+ fileName,
////								path + File.separator + "results" + File.separator+ fileName.replace("mp4", "flv"));
////
////						map.put(path + File.separator + "results" + File.separator+ fileName.replace("mp4", "flv"),"video/video"+
////								projectNum+ File.separator + fileName.replace("videos/", "").replace("mp4", "flv"));
////					}
////				}
//			}
//			System.out.println(map.size());
			UploadToOssUtil uploadToOssUtil = new UploadToOssUtil();
//			uploadToOssUtil.uploadMulFiles(map);

			uploadToOssUtil.uploadS3File("F:\\文档\\下载\\result (3).png", "images/imageseur-EBajWJTgS/result.png");
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	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;
	}

}
