UploadUtils(对象存储OSS /阿里云的代码 上传网络流)
package com.javasm.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.PutObjectRequest; import com.aliyun.oss.model.PutObjectResult; import lombok.extern.slf4j.Slf4j; import javax.servlet.http.Part; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; /** * @Author:Zxb * @Version:1.0 * @Date:2023/3/22-9:46 * @Since:jdk1.8 * @Description: */ @Slf4j public class UploadUtils { public static String upload(Part part){ //阿里云的代码 上传网络流 // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。 String endpoint = "https://oss-cn-nanjing.aliyuncs.com"; // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。 String accessKeyId = "自己阿里云取LTAI5tAtKQTB9Fg63NWkQTdQ"; String accessKeySecret = "自己阿里云取nzHvrDxnBepLaA109KGRfWWS1vm6Oj"; // 填写Bucket名称,例如examplebucket。 String bucketName = "wuhanjava17"; // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。 //获取文件名 String fileName = part.getSubmittedFileName(); OSS ossClient = null; try { // 填写文件流 InputStream inputStream = part.getInputStream(); // 创建OSSClient实例。 ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 创建PutObjectRequest对象。 PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream); // 设置该属性可以返回response。如果不设置,则返回的response为空。 putObjectRequest.setProcess("true"); // 创建PutObject请求。 PutObjectResult result = ossClient.putObject(putObjectRequest); // 如果上传成功,则返回200。 log.info(result.getResponse().getStatusCode()+""); } catch (OSSException oe) { log.info("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); log.info("Error Message:" + oe.getErrorMessage()); log.info("Error Code:" + oe.getErrorCode()); log.info("Request ID:" + oe.getRequestId()); log.info("Host ID:" + oe.getHostId()); } catch (ClientException ce) { log.info("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."); log.info("Error Message:" + ce.getMessage()); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (ossClient != null) { ossClient.shutdown(); } } return "https://wuhanjava17.oss-cn-nanjing.aliyuncs.com/"+fileName; } }
UploadController
@RestController public class UploadController { @PostMapping("upload") public AxiosResult<String> upload(@RequestPart Part part){ //进行文件上传 UploadUtil String upload = UploadUtils.upload(part); return AxiosResult.success(upload); } }
TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back 标签:info,文件,log,com,oss,阿里,import,上传,String From: https://www.cnblogs.com/sibuxiang-zxb/p/17245739.html