springboot注解方式上传图片url到数据库

实体类有id,name,detail,picture。其中picture是的String类。如何设计Java代码实现这个接口,可以将图片的url传到数据库,代码层次如图,来个大佬帮帮忙吧

img

大概思路就是你本地保存图片,数据库存储你保存图片的地址
/*
博客图片保存绝对地址
*/
@Value("${imgaes.save.path}")
private String filePath;

/*
博客映射的相对地址
 */
@Value("${imgaes.path.relative}")
private String filePathRe;




public String imgageUp(MultipartFile file){


    /*
    文件不能为空
     */
    if(file.isEmpty()){
        System.out.println("文件为空");
        return " ";
    }
    /*
    为映射数据库和保存地址准备
     */
    String fileName=file.getOriginalFilename();//获取文件名
    String suffixName=fileName.substring(fileName.lastIndexOf("."));//获取后缀名
    //String filePath="E://Idea workspace//demoweb2//src//main//resources//static//images//image//";

    fileName= UUID.randomUUID().toString().substring(0,4)+suffixName;//新图片名
    File dest=new File(filePath+fileName); //创建新文件
    if(!dest.getParentFile().exists()){
        dest.getParentFile().mkdirs();

    }
    try {
        file.transferTo(dest); //保存图片

        System.out.println("file = " + "图片保存成功");
    } catch (IOException e) {
        e.printStackTrace();
    }

    String NewfiLiePath=filePathRe+fileName;//映射到数据库的地址

    return NewfiLiePath;
}

图片路径保存的时候保存路径不就可以了

可以用输入流输出流,前端上传图片把A文件下的a.jpg上传到B文件下这样子