后端实现图片上传本地,可采用url查看图片

前言

本文将实现在数据库中存储图片url,url可以在浏览器中访问查看。
整体思路为:

  • 上传图片到本地指定地址
  • 为图片分配url保存至数据库
  • 根据分配url,进行物理地址映射到本地指定地址

具体实现

controller层:
上传图片到本地指定地址,为图片分配url保存至数据库

@PostMapping("/uploadImg")
    @Operation(summary = "上传图片")
    public Result<String> upload(MultipartFile file, HttpServletRequest request) {

        String subPath = "\\upload\\Img\\";
        String basepath = webFileUrl;
        String path = "";
        String vpath = "";
        String fileType = "";
        Map<String, Object> resultPath = WebFileUtils.saveFile(file, basepath + subPath, request);
        path = resultPath.get("path").toString();
        fileType = resultPath.get("fileType").toString();
        vpath = "/statics/upload/" + "Img" + "/" + fileType + "/" + new File(path).getName();

        return R2.ok(vpath,"上传成功");
    }

其中的WebFileUtils类:

public static Map<String, Object> saveFile(MultipartFile file, String webFileUrl, HttpServletRequest request) {
        System.out.println();
        String realPath, rootPath = webFileUrl;

        if (webFileUrl.equals("")) {
            //设置主路径
            realPath = request.getServletContext().getRealPath("");
            //将路径设置到当前项目同级,并创建upload文件夹
            rootPath = new File(realPath).getParent() + "\\upload\\";
        }
        //组合文件名
        String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("\\") + 1);
        //获取文件后缀
        String extension = "." + FilenameUtils.getExtension(filename);
        //生成新的文件名
        String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + UUID.randomUUID().toString().replace("-","") + extension;

        // 区分上传文件是图片还是视频
        String fileType;
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = fileNameMap.getContentTypeFor(filename);
        if (contentTypeFor == null){
            fileType = "video";
            rootPath = rootPath + "\\" + fileType +"\\";
        }else{
            fileType = "image";
            rootPath = rootPath + "\\" + fileType +"\\";
        }

        String filePath = rootPath + newFileName;
        File localFile = new File(filePath);

        // 检测是否存在目录,不存在则创建
        if (!localFile.getParentFile().exists()) {
            localFile.getParentFile().mkdirs();
        }
        try {
            //保存文件
            file.transferTo(localFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String,Object> result = new MapUtils();
        result.put("path", localFile.getAbsolutePath());
        result.put("fileType", fileType );
        //返回文件的绝对路径
        return result;
    }

此处webFileUrl,替换为上传文件本地保存地址。

config:
在此处完成物理地址映射到本地指定地址

@Configuration
public class UploadConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/statics/upload/Img/image/**")
                .addResourceLocations("file:本地保存地址");
    }
}

至此,如果需要实现前端图片预览查看功能,请求数据库中保存的图片url即可。

相关推荐

  1. 实现图片本地采用url查看图片

    2024-07-17 22:44:02       22 阅读
  2. Unity UnityWebRequest 向php图片文件

    2024-07-17 22:44:02       57 阅读
  3. uniapp 多张图片到django

    2024-07-17 22:44:02       50 阅读
  4. el-upload图片给SpringBoot

    2024-07-17 22:44:02       36 阅读
  5. Egg框架搭建服务【6】- 图片图片回显

    2024-07-17 22:44:02       59 阅读
  6. 前端反显图片预览图片

    2024-07-17 22:44:02       19 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 22:44:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 22:44:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 22:44:02       58 阅读
  4. Python语言-面向对象

    2024-07-17 22:44:02       69 阅读

热门阅读

  1. 总览

    总览

    2024-07-17 22:44:02      18 阅读
  2. C4D S26新功能完整列表

    2024-07-17 22:44:02       25 阅读
  3. 大模型日报 2024-07-17

    2024-07-17 22:44:02       25 阅读
  4. 卡码网语言基础课 | 5. A+B问题⑤

    2024-07-17 22:44:02       23 阅读
  5. Web前端-Web开发CSS基础2-选择器

    2024-07-17 22:44:02       17 阅读
  6. 448. 找到所有数组中消失的数字

    2024-07-17 22:44:02       20 阅读
  7. 洛谷P1255 数楼梯

    2024-07-17 22:44:02       20 阅读
  8. C#后台向某个网站发送Get或者Post请求

    2024-07-17 22:44:02       26 阅读
  9. c#中的事件

    2024-07-17 22:44:02       24 阅读
  10. 用python写一个爬虫,爬取google中关于蛇的照片

    2024-07-17 22:44:02       21 阅读