package com.manageSystem.common.utils; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.PutObjectResult; import com.aliyun.oss.model.UploadFileRequest; import com.aliyun.oss.model.UploadFileResult; import com.manageSystem.framework.web.domain.AjaxResult; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.xml.crypto.Data; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; import java.text.DecimalFormat; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * * @author cky * 代码来源阿里官网 * 略有改动 */ @Component public class OssFileUpload { @Value("${oss.endPoint}") String endpoint; @Value("${oss.accessKeyId}") String accessKeyId = "yourAccessKeyId"; @Value("${oss.accessKeySecret}") String accessKeySecret = "yourAccessKeySecret"; @Value("${oss.bucketName}") String bucketName ; public List<URL> upload(MultipartFile[] files) throws IOException { MultipartEntityBuilder builder = MultipartEntityBuilder.create(); DecimalFormat df = new DecimalFormat("#.##"); HttpEntity multipart = builder.build(); // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { List<URL> list = new ArrayList<>(); for(MultipartFile multipartFile : files) { String objectName = LocalDate.now()+"/" +multipartFile.getOriginalFilename(); PutObjectResult putObjectResult = ossClient.putObject(bucketName,objectName,new ByteArrayInputStream(multipartFile.getBytes())); Date date = new Date(System.currentTimeMillis()+1000*60*60*24*7); URL url = ossClient.generatePresignedUrl(bucketName, objectName,date ); list.add(url); } return list; } catch (OSSException oe) { oe.printStackTrace(); } catch (ClientException ce) { ce.printStackTrace(); } catch (Throwable throwable) { throwable.printStackTrace(); } finally { if (ossClient != null) { ossClient.shutdown(); } } return null; } public String getSuffix(final MultipartFile file) { if (file == null || file.getSize() == 0) { return null; } String fileName = file.getOriginalFilename(); return fileName.substring(fileName.lastIndexOf(".") + 1);
} }
@ApiOperation("OSS文件上传") @PostMapping(value = "/common/oss/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public AjaxResult uploadFile2(@RequestPart("file") MultipartFile[] file) { try { List<URL> upload = ossFileUpload.upload(file); return AjaxResult.success(upload); }catch (Exception e){e.printStackTrace();} return AjaxResult.error("oss上传失败"); }
标签:文件,java,com,oss,aliyun,org,import,上传 From: https://www.cnblogs.com/wangweiip/p/16952925.html