springboot存图片到本地并生成url/映射静态资源路径

导入模块

 
            cn.hutool
            hutool-all
            5.7.7
        

控制器接收

  //   批量添加书本图片
    @PostMapping("/addBookImages")
    public Boolean addBookImage(@RequestPart MultipartFile[] images) throws IOException {
        booksService.getImages(images);
    
        return null;
    }

保存图片并生成url

package com.bookstore.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static com.bookstore.util.FileToMultipartFile.fileToMultipartFile;

public class ImageManage {

    //    修改用户头像
    public static String saveHeadImage( MultipartFile image) throws IOException {
        String originalFilename = image.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        // 定义一个文件唯一的标识码
        String fileUUID = IdUtil.fastSimpleUUID() + StrUtil.DOT + type;
        File uploadFile = new File("E:/毕设_网上书店/bookstore_static/images/headImage/" + fileUUID);
        System.out.println("uploadFile:"+uploadFile);
        // 上传文件到磁盘
            image.transferTo(uploadFile);
        // 数据库若不存在重复文件,则不删除刚才上传的文件
        String url = "http://localhost:9527/static/headImage/" + fileUUID;
        return url;
    }


    //    修改商品图片
    public static String saveCommomdityImage( MultipartFile image) throws IOException {
        String originalFilename = image.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        System.out.println(type);
        if(type.equals(""))
            type="jpg";

        // 定义一个文件唯一的标识码
        String fileUUID = IdUtil.fastSimpleUUID() + StrUtil.DOT + type;
        File uploadFile = new File("E:/毕设_网上书店/bookstore_static/images/books/" + fileUUID);
        System.out.println("uploadFile:"+uploadFile);
        // 判断配置的文件目录是否存在,若不存在则创建一个新的文件目录
        File parentFile = uploadFile.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        // 上传文件到磁盘
            image.transferTo(uploadFile);
        // 数据库若不存在重复文件,则不删除刚才上传的文件
        String url = "http://localhost:9527/static/books/" + fileUUID;
        return url;
    }


}


springboot映射静态资源路径(映射对应地址后才能用上面的url访问静态资源)


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class BookstoreApplication implements WebMvcConfigurer {

    public static void main(String[] args) {

        SpringApplication.run(BookstoreApplication.class, args);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
//addResourceLocations:存放静态资源的路径    
//addResourceHandler:映射后的访问路径
        registry.addResourceHandler("/static/headImage/**").addResourceLocations("file:E:/毕设_网上书店/bookstore_static/images/headImage/");
        registry.addResourceHandler("/static/books/**").addResourceLocations("file:E:/毕设_网上书店/bookstore_static/images/books/");

    }
}

 

 

你可能感兴趣的:(spring,boot,java,spring)