赞
踩
请自行替换秘钥:
#阿里云 OSS src/main/resources/application.properties #不同的服务器,地址不同 aliyun.oss.file.endpoint=https://oss-cn-hangzhou.aliyuncs.com aliyun.oss.file.accessKeyId=LTAI5t9wUqCoD42qPGRy8S aliyun.oss.file.accessKeySecret=WV9W6erbdF2oqH4KIlo5BzNrwaP #bucket可以在控制台创建,也可以使用java代码创建 aliyun.oss.file.bucketName=zhurenjin-workspace
多个文件批量上传,单个上传,工具类:
- public Map<String, Object> Upload(MultipartFile file) throws IOException {
- InputStream inputStream = file.getInputStream();
-
- // 避免文件覆盖
- String originalFilename = file.getOriginalFilename();
- int iPoint = originalFilename.lastIndexOf(".");
- String fileTypePre = originalFilename.substring(0,iPoint);
- String fileType = originalFilename.substring(iPoint);
- String fileName = UUID.randomUUID().toString()+fileTypePre+fileType;
-
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
- // PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
- // 创建PutObject请求。
- ossClient.putObject(bucketName,fileName,inputStream);
- // https://zhurenjin-workspace.oss-cn-hangzhou.aliyuncs.com/exampledir/exampleobject.jpg
- String imgUrl = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/"+fileName;
- ossClient.shutdown();
-
- Map<String ,Object> re = new HashMap<String ,Object>();
- Map<String ,Object> imgList = new HashMap<String ,Object>();
- imgList.put("imageUrl",imgUrl);
- re.put("code",200);
- re.put("msg","上传成功");
- re.put("data",imgList);
-
- return re;
-
- }
单个文件上传
- @PostMapping("uploadAliyunOSSByUtils")
- public AjaxResult uploadAliyunOSSByUtils( @RequestParam("files") MultipartFile img) throws IOException {
- if (!(img.getOriginalFilename().endsWith(".png")) && !(img.getOriginalFilename().endsWith(".jpg")) && !(img.getOriginalFilename().endsWith(".PNG"))) {
- try {
- return AjaxResult.error("文件类型错误,只能为png或者jpg");
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- // 调用阿里云工具类进行文件上传
- AliOSSUtils aliOSSUtils = new AliOSSUtils();
- Map<String, Object> upload = aliOSSUtils.Upload(img);
- return AjaxResult.success(upload);
- }
多个文件上传
- // 阿里云存储(多张图片)
- @PostMapping("uploadAliyunOSSByUtilsArrayFils")
- public AjaxResult uploadAliyunOSSByUtilsArrayFils( MultipartFile[] imgList) throws IOException {
- Map<String, Object> objectObjectHashMap = new HashMap<>();
- // List<Object> objectsImages = new ArrayList<>();
- List<Map<String, Object>> objectsImages = new ArrayList<>();
- // 调用阿里云工具类进行文件上传
- for (MultipartFile img : imgList){
- Map<String, Object> uploadImd = aliOSSUtils.Upload(img);
- Map<String, Object> New_ = new HashMap<>();
- New_.put("strImg",uploadImd);
- objectsImages.add(New_);
- // objectsImages.add(uploadImd);
- }
- return AjaxResult.success(objectsImages);
- }
单个删除
- // 阿里云删除单张图片
- @PostMapping("deleteAliyunOSSByUtils")
- public AjaxResult deleteAliyunOSSByUtils(@RequestBody Map<String,String> img) throws IOException {
- // 创建OSSClient实例。
-
- System.out.println(img);
- boolean b = aliOSSUtils.deleteFile(img);
- return AjaxResult.success(b);
- }
多个删除
- @PostMapping("deleteAliyunOSSByUtilsArrayList")
- public AjaxResult deleteAliyunOSSByUtilsArrayList(@RequestBody List<String> imgList) throws IOException {
- // 创建OSSClient实例。
-
- boolean b = aliOSSUtils.deleteAllFile(imgList);
- return AjaxResult.success(b);
- }
- /*删除工具类《单个》*/
- public boolean deleteFile(Map<String,String> fileUrl){
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
-
- String img = fileUrl.get("img");
- int begin = (endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/").length(); //找到文件路径的开始下标
- String deleteUrl = img.substring(begin);
- try {
- // 删除文件请求
- VoidResult voidResult = ossClient.deleteObject(bucketName, deleteUrl);
- System.out.println(voidResult+"删除chengg11111??222");
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
- /*删除工具类《多个》,主要就是要获取到相应的文件后缀,String deleteUrl = objImg.substring(begin);*/
- public boolean deleteAllFile(List<String> keys){
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
- List<String> keysBox= new ArrayList<>();
- int begin = (endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/").length(); //找到文件路径的开始下标
- for(String objImg : keys) {
- String deleteUrl = objImg.substring(begin);
- keysBox.add(deleteUrl);
- }
- try {
- DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keysBox).withEncodingType("url"));
- List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
- try {
- for(String obj : deletedObjects) {
- String deleteObj = URLDecoder.decode(obj, "UTF-8");
- System.out.println(deleteObj);
- }
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- } finally {
- if (ossClient != null) {
- ossClient.shutdown();
- }
- }
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。