Springboot文件上传--资源映射

关于Java的文件上传有很多种方式,可以直接上传本地,或者上传七牛云,或者搭个HDFS文件系统,但是后者相对麻烦。本文采用的方式是使用springboot进行文件上传,并进行资源映射,可直接通过本地服务访问存储本地的图片。

 

application.yml文件中,配置统一的存储文件路径。包括本地路径和返回给前端的路径

ueditor:
  config: config.json
  unified: true
  upload-path : D:/upload/
  url-prefix : http://172.0.0.1:8080/upload/

写个配置类UEditorConfig,记录yml文件里面的地址

@ConfigurationProperties(prefix = "ueditor")
public  class UEditorConfig {

    /**
     * config.json的文件存放地址
     */
    private String config;
    /**
     * 是否同统一上传地址:图片上传地址,视频上传地址...
     */
    private boolean unified;
    /**
     * 文件上传路径
     */
    private String uploadPath;
    /**
     * 文件url前缀
     */
    private String urlPrefix;

    public String getConfig() {
        return config;
    }

    public void setConfig(String config) {
        this.config = config;
    }

    public String getUploadPath() {
        return uploadPath;
    }

    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
    }

    public String getUrlPrefix() {
        return urlPrefix;
    }

    public void setUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

    public boolean getUnified() {
        return unified;
    }

    public void setUnified(boolean unified) {
        this.unified = unified;
    }
}

 

接着自己写个类实现WebMvcConfigurer

@EnableWebMvc
@ServletComponentScan
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {



    @Autowired
    private UEditorConfig uEditorConfig;
 /**
     * 本地资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//这行是关键,用于将本地的图片映射到服务器上,此时访问http://172.0.0.1:8080/upload/下的图片就相当于直接访问你D盘下的图片
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uEditorConfig.getUploadPath());

    }
}

 

接着写个文件上传的控制层

@PostMapping("/upload")
public Object upload(MultipartFile file) throws Exception {
    return iImagesService.upload(file );

}

Service实现层

 

@Autowired
private UEditorConfig uEditorConfig;
  @Override
    public ResultMap upload(MultipartFile file) throws Exception {
        if (file==null){
            return ResultUtil.error("图片为空");
        }
        if (file.getSize()>2048000){
            return ResultUtil.error("图片大小不能超过2M");
        }
        String prefix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
        List names= Arrays.asList("bmp", "png", "jpeg", "jpg", "gif");
        boolean noneMatch = names.stream().noneMatch(s -> s.contains(prefix));
        if (noneMatch){
            return ResultUtil.error("图片只支持bmp/png/jpeg/jpg/gif格式");
        }
        String realPath= uEditorConfig.getUploadPath();
        String voPath= uEditorConfig.getUrlPrefix();
        log.info(realPath);
        File tmpfile = new File(realPath); // 检查目录是否存在
        if (!tmpfile.exists()) {
            tmpfile.mkdirs();// 如果文件目录不存在,则新建目录
        }
        String fileName=new Date().getTime() +"."+ prefix;
        File localFile = new File(tmpfile.getAbsolutePath(), fileName);
        file.transferTo(localFile);



        return ResultUtil.success();
    }

 

至此文件已经可成功上传至本地D盘的upload目录下,通过服务器http://172.0.0.1:8080/upload/可以直接访问本地的图片了

你可能感兴趣的:(Java,文件上传,springboot)