美文网首页
spring boot 图片上传到本地目录

spring boot 图片上传到本地目录

作者: 周六不算加班 | 来源:发表于2019-01-03 13:57 被阅读30次

1、图片上传

public Object localUpload(HttpServletRequest request,String uploadpath){
    MultipartHttpServletRequest mReq = (MultipartHttpServletRequest)request;
    MultipartFile file = mReq.getFile("file");
    String fileName= file.getOriginalFilename();
    //重新命名图片文件
    fileName = fileUtils.reName(fileName);
    File localFile = new File(uploadpath,fileName);
    if(!localFile.getParentFile().exists()){ //判断文件父目录是否存在
        localFile.getParentFile().mkdir();
    }
    try {
        file.transferTo(localFile);
        return "上传成功";
    }catch (Exception e){
        return "上传失败";
    }

}

2、图片访问(资源映射)

配置文件中设置
#访问服务器本地资源

  web.upload-path=D:/upload/
  spring.mvc.static-path-pattern=/**
  spring.resources.static-locations=classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/static/,classpath\:/public/,file\:${web.upload-path} classpath:/static/,classpath:/public/,file:${web.upload-path}

相关文章

网友评论

      本文标题:spring boot 图片上传到本地目录

      本文链接:https://www.haomeiwen.com/subject/pbyfrqtx.html