springboot2.0上传文件生成请求路径给前台

配置yml

spring:
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
files:
  #下载文件虚拟路径(服务器ip)
  staticUrl: http://192.168.1.142:8080
  staticPath: /base/files/download
  #文件真实路径
  basePath: E:/real/file
  #用户模块
  user: /user/      

映射虚拟路径与文件的真实路径

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //配置虚拟映射路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/base/files/download/**")
                .addResourceLocations("file:E:/real/file/");
    }
}

路径工具类

/**
 * 文件上传配置
 */
@Component
@ConfigurationProperties(prefix = "files")
public class FileConfig {

    //用户模块
    public static final String user_file_path="user";

    //用户统一头像路径
    public static final String picture="picture";

    private static String staticUrl;
    private static String staticPath;
    private static String basePath;
    private static String user;

    public static String getStaticUrl() {
        return staticUrl;
    }

    public void setStaticUrl(String staticUrl) {
        this.staticUrl = staticUrl;
    }

    public static String getStaticPath() {
        return staticPath;
    }

    public void setStaticPath(String staticPath) {
        this.staticPath = staticPath;
    }

    public static String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    public static String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }


    /**
     * 用户上传文件地址
     * @param type
     * @param key
     * @return
     */
    public static String getFileUpLoadPath(String type,String key){
        switch (type){
            case FileConfig.user_file_path:
                return getBasePath()+getUser()+key+"/";
            default:
                return getBasePath()+"/other/"+key+"/";
        }
    }

    /**
     * 获取各个模块完整的下载路径
     * @param type
     * @param key
     * @param fileName
     * @return
     */
    public static String getDownLoadPath(String type,String key,String fileName){
        switch (type){
            case FileConfig.user_file_path:
                return getStaticUrl()+getStaticPath()+getUser()+key+"/"+fileName;
             default:
                 return getStaticUrl()+getStaticPath()+"other"+key+"/"+fileName;
        }
    }

}

contorller上传方法

@RestController
@RequestMapping("/user")
public class UserController {


    @PostMapping(value = "/uploadUserHeadPicture")
    public Map<String,Object> uploadUserHead(@RequestParam("file") MultipartFile file){
        //获取文件上传路径
        String picDir=FileConfig.getFileUpLoadPath(FileConfig.user_file_path,FileConfig.picture);
        File dir=new File(picDir);
        if (!dir.exists()){
            dir.mkdirs();
        }
        Map<String,Object> map=new HashMap<>();
        String fileName= UUID.randomUUID().toString()+".jpg";
        try {
            file.transferTo(new File(picDir+fileName));
            String downLoadPath=FileConfig.getDownLoadPath(FileConfig.user_file_path,FileConfig.picture,fileName);
            map.put("message","success");
            map.put("url",downLoadPath);
            return map;
        } catch (IOException e) {
            map.put("message","error");
            map.put("url","");
            return map;
        }
    }

}

postman测试

在这里插入图片描述

效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值