#服务配置file:staticPath: /upload/**uploadPath: E:/temp/
server:port:8080spring:profiles:active: dev
OSS文件上传 需要去阿里云申请一下OSS服务
packagecom.zhang.upload_demo02.service;importcom.aliyun.oss.ClientException;importcom.aliyun.oss.OSS;importcom.aliyun.oss.OSSClientBuilder;importcom.aliyun.oss.OSSException;importorg.springframework.stereotype.Service;importorg.springframework.web.multipart.MultipartFile;importjava.io.*;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.UUID;@ServicepublicclassOssUploadService{publicStringuploadfile(MultipartFile multipartFile,String dir){// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。关于其他Region对应的Endpoint信息,请参见访问域名和数据中心。String endpoint ="oss-cn-beijing.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。//LTAI5t73fB85acU8TfeE9io2//6Tg0RyOfeyVTebD209Y45O13Bwaoz8String accessKeyId ="***";//安全密钥用户名String accessKeySecret ="***";//安全密码// 填写Bucket名称,例如examplebucket。String bucketName ="zitbookings";// 创建OSSClient实例。OSS ossClient =null;try{
ossClient =newOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 1.获取文件上传流InputStream inputStream = multipartFile.getInputStream();// 2.构建日期目录SimpleDateFormat sdf =newSimpleDateFormat("yyyy/MM/dd");String dataPath = sdf.format(newDate());// 4.获取文件真实名字 并重构String originalFilename = multipartFile.getOriginalFilename();String filename = UUID.randomUUID().toString();String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));String newName = filename + suffix;String fileUrl = dir +"/"+ dataPath + newName;// 5.文件上传到阿里云云服务器
ossClient.putObject(bucketName, fileUrl, inputStream);return"http://"+ bucketName +"."+ endpoint +"/"+ fileUrl;}catch(OSSException oe){System.out.println("Caught an OSSException, which means your request made it to OSS, "+"but was rejected with an error response for some reason.");System.out.println("Error Message:"+ oe.getErrorMessage());System.out.println("Error Code:"+ oe.getErrorCode());System.out.println("Request ID:"+ oe.getRequestId());System.out.println("Host ID:"+ oe.getHostId());return"fail";}catch(ClientException ce){System.out.println("Caught an ClientException, which means the client encountered "+"a serious internal problem while trying to communicate with OSS, "+"such as not being able to access the network.");System.out.println("Error Message:"+ ce.getMessage());return"fail";}catch(IOException e){
e.printStackTrace();return"fail";}finally{if(ossClient !=null){
ossClient.shutdown();}}}}