使用Mavon-Editor编辑器上传本地图片到又拍云云存储(Vue+SpringBoot)

需求:将本地的图片上传到服务器或者云存储中,考虑之后,这里我选的是上传到又拍云云存储。

复制代码
技术背景:
前端:Vue+Ajax
后端:SpringBoot
存储:又拍云云存储

原理:Mavon-Editor编辑器有两个重要的属性和事件

复制代码
ref="md"     
@imgAdd="imgAdd"

所以需要将用到Mavon-Editor编辑器的组件中添加上这个属性和事件。

然后在方法中定义imgAdd函数

代码片段:

javascript 复制代码
 // 上傳圖片script部分
    imgAdd(pos, file) {
      //这里的pos指的是在数组中的下标
      //这里创建FormData对象并将从本地获取到的file值存入。
      var formdata = new FormData();
      formdata.append("file", file);

      getimgurl(formdata)
        .then((response) => {
          // 请求成功,获取后端返回的字符串数据
        //   const url = response;
        //   console.log("返回的字符串数据:", response);
		  this.$refs.md.$img2Url(pos, response);//直接賦值就好了
        })
        .catch((error) => {
          // 请求失败,处理错误
          console.error("请求失败0000:", error);
        });

     
    },

其中请求路径是你自己的后端

例如:http:localhost:8080/admin/xxx

后端需要做的:

1:导包

javascript 复制代码
<!--        又拍云配置-->
        <dependency>
            <groupId>com.upyun</groupId>
            <artifactId>java-sdk</artifactId>
            <version>4.2.3</version>
        </dependency>

2:编写控制层处理前端请求

java 复制代码
import com.UpYun;
import com.upyun.UpException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

/**
 * 功能描述
 *
 * @author:
 * @date: 2023年08月20日 15:37
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE},
        allowedHeaders = {"Content-Type", "Authorization"})
public class ImgUpload {
    @Value("${upload.upyun.bucket-name}")
    private String bucketName;
    @Value("${upload.upyun.username}")
    private String username;
    @Value("${upload.upyun.password}")
    private String password;
    //上面的可以直接写死,不需要注入,
    @PostMapping("imgAdd")
    // 这个路径就是前端发post请求的路径
    public String uploadImg( MultipartFile file) throws UpException, IOException {
        // 拼接文件名
//        System.out.println("进来了"+file);
        String trueFileName =file.getOriginalFilename();
//        String suffix = trueFileName .substring(trueFileName .lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+trueFileName;

        // 上传到又拍云

        UpYun upYun=new UpYun(bucketName,username,password);
//        UpYun upYun=new UpYun("空间名","操作员名称","操作员密码");
        String dirPath="/img/blog-img/";

        //  为每个文章创建了一个文件夹
        boolean mkDir = upYun.mkDir(dirPath);
        if(mkDir){
//            System.out.println(dirPath+fileName);
            upYun.writeFile(dirPath+fileName,file.getBytes(),false);

            String url="https://你的域名"+dirPath+fileName;
            System.out.println("url: "+url.toString());
            return url;
        }else {
            return "后端:图片上传失败";
        }

    }

}

主要思想:就是前端将本地上传的文件通过发送Ajax请求传递给后台,后台将图片上传到云存储中,然后再向前端返回图片存储的路径,这样就能正常显示在界面上了。

相关推荐
2301_32241428046 小时前
基于SpringBoot+Vue的休闲零食超市管理系统的设计与实现59004-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·vue.js·spring boot·redis·mysql·决策树·推荐算法
system_error_x10 小时前
SpringBoot 集成 Thymeleaf 简洁入门
java·spring boot·后端
酷炫码神12 小时前
Day 8|Vue3 响应式:告别手动操作 DOM,让数据驱动界面
javascript·vue.js·ecmascript
星光不问赶路人14 小时前
Vue 3 对象监听与快照:ref 和 reactive 的关键差异
前端·vue.js
十年Java程序媛14 小时前
Java this 与 super 详解|继承高频踩坑 + 面试题整理
java·spring boot·面试
稻草人47814 小时前
Vue3.0项目创建步骤
vue.js
工具派14 小时前
不重写一行老代码:给 Spring Boot 系统加一层 AI 能力
java·人工智能·spring boot
aiks_yuan14 小时前
RestTemplate 废弃后迁移 RestClient 实战:Spring Boot 4.2 升级指南
spring boot
Bug收容所15 小时前
Vue+js Day2
前端·javascript·vue.js
aibo86030815 小时前
基于 Spring Boot 的茶铺管理系统设计与实现
java·spring boot·后端