• java本地开发上传图片可见两种处理办法


    方法一:使用addResourceHandlers()

    1. @Configuration
    2. public class MyWebConfig implements WebMvcConfigurer {
    3. @Value("${file.path}")
    4. String filepath;
    5. @Value("${file.prefix}")
    6. String fileprefix;
    7. @Override
    8. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    9. /**
    10. * 访问路径是addResourceHandler()中的路径,映射到访问本地的addResourceLocations()中
    11. */
    12. registry.addResourceHandler(fileprefix + "**").addResourceLocations("file:///" + filepath);
    13. }
    14. }

    yml配置

    1. 本地的
    2. file:
    3. path: D:/heroesupload/
    4. prefix: /upload/
    5. 线上的
    6. file:
    7. path: /heroesupload/
    8. prefix: /upload/

    上传的后台代码

    保存的图片的 路径是  /upload/xxx.png,这样自己方便

    其实开始本想只想保存  xxx.png

    1. ps: 控制台就只放部分代码了
    2. @Value("${file.path}")
    3. private String filepath;
    4. String tempFileName = fileUtil.fileUpload(file, filepath);
    5. ##########################################
    6. @Configuration
    7. public class FileUtil {
    8. @Value("${file.prefix}")
    9. String fileprefix;
    10. /**
    11. * @Author lanren312
    12. * @Description: 单个文件上传
    13. * @param file 文件对象
    14. * @param path 文件存储的路径
    15. * @date 2022/2/6 22:31
    16. */
    17. public String fileUpload(MultipartFile file, String path) throws IOException {
    18. String name = file.getOriginalFilename();
    19. String suffixName = name.substring(name.lastIndexOf("."));
    20. String fileName = UUIDUtil.uuid() + suffixName;
    21. File tempFile = new File(path, fileName);
    22. if (!tempFile.getParentFile().exists()) {
    23. tempFile.getParentFile().mkdir();
    24. }
    25. if (tempFile.exists()) {
    26. tempFile.delete();
    27. }
    28. tempFile.createNewFile();
    29. file.transferTo(tempFile);
    30. return fileprefix + tempFile.getName(); // 这里多了一个 fileprefix, 也就是 /uplaod
    31. }
    32. }

    在这里说明下,yml中的配置后都以 ‘/’ 结尾方便,如果不要,有些地方就要 File.separator,比如上面的代码就要这样写  

    return fileprefix + File.separator + tempFile.getName();

    F12看看查询出来的staffIcon,再去看看addResourceHandlers()里面的配置,就应该明白了吧,图片显示成功。

    本地可以,放到云服务器测试也是ok的。 

     方法二:使用tomcat

    1、将本地的tomcat添加到服务并启动

    不熟的可以看看我之前写的 windows创建及删除redis、mysql服务_lanren312的博客-CSDN博客_删除redis服务

     或者本地临时启动应该也是可以的。

    2、配置yml

    tomcat安装目录中新建文件夹 heroesupload

    D:\Program Files\apache-tomcat-8.5.56\webapps\heroesupload
    1. 本地的
    2. file:
    3. path: D:\Program Files\apache-tomcat-8.5.56\webapps\heroesupload\
    4. prefix: http://机器ip:8080/heroesupload/
    5. 线上的
    6. file:
    7. path: /data/vue/heroesupload/
    8. prefix: http://服务器ip/heroesupload/ #使用了nginx配置/data/vue

     这样配置保存的图片的路径就可以写成  /xxx.png,更简洁

    说明下,本地的 filepath是下载路径,fileprefix是查看路径,这个你应该懂吧

    线上的也是一样,可以配合nginx用,tomcat也行

    如果使用tomcat配置,千万千万千万要启动tomcat,重要的事说三遍!!!

  • 相关阅读:
    C++复合类型总结,看这一篇就够了
    知识付费直播间即时通讯
    81. this、call、apply、bind?
    Day33力扣打卡
    STP生成树(端口状态+端口角色+收敛机制 )|||| STP优化技术( uplinkfast技术+Portfast技术+backbonefast技术 )详解
    【MySQL 第一天安装教程】
    mysql面试题32:MySQL数据库服务器性能分析的方法命令有哪些?
    Spring 源码阅读(二)IoC 容器初始化以及 BeanFactory 创建和 BeanDefinition 加载过程
    页面搭建系统的那些事儿
    字符编码复习
  • 原文地址:https://blog.csdn.net/lanren312/article/details/126515191