当前位置: 首页 > news >正文

将图片资源保存到服务器的盘符中

服务类

  • 系统盘符:file-path.disk(可能会变,配置配置文件dev中)
  • 文件根路径:file-path.root-path(可能会变,配置配置文件dev中)
  • http协议的Nginx的映射前缀:PrefixConstant.HTTP_PREFIX
package com.sky.service;import com.sky.constant.MessageConstant;
import com.sky.constant.PrefixConstant;
import com.sky.exception.BaseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.File;
import java.io.IOException;@Service
public class CommonService {@Value("${file-path.disk}")private String disk;@Value("${file-path.root-path}")private String rootPath;/*** 文件上传** @param parentPath 父路径* @param file       文件*/public String upload(String parentPath, MultipartFile file) {// Upload fileString fileName = file.getOriginalFilename();// Get full path using system file separatorString fullPath = disk + ":" + File.separator + rootPath + File.separator + parentPath;// Create the directory if it doesn't existFile directory = new File(fullPath);if (!directory.exists()) {directory.mkdirs();}// Create the destination fileFile dest = new File(directory, fileName);try {file.transferTo(dest);} catch (IOException e) {throw new BaseException(MessageConstant.UPLOAD_FAILED);}// Return the file access URLreturn PrefixConstant.HTTP_PREFIX +fileName;}
}

配置文件

在这里插入图片描述

总配置文件

sky:jwt:# 设置jwt签名加密时使用的秘钥admin-secret-key: itcast# 设置jwt过期时间admin-ttl: 720000000# 设置前端传递过来的令牌名称admin-token-name: tokenfile-path:disk: ${file-path.disk}root-path: ${file-path.root-path}

开发环境配置文件

file-path:disk: Droot-path: sky_take_out

常量类

在这里插入图片描述

文件路径常量

package com.sky.constant;public class SystemPathConstant {public static final String COMMON = "common";// 构造函数私有化private SystemPathConstant() {}
}

nginx路径映射常量

package com.sky.constant;public class PrefixConstant {public static final String HTTP_PREFIX = "http://localhost/files/";// 构造函数私有化private PrefixConstant() {}
}

controller层

别人上传完图片之后把访问路径给别人(http的路径,不是本地路径)

    @PostMapping("/upload")@ApiOperation("文件上传")public Result<String> upload(MultipartFile file) {log.info("文件上传:{}", file);// 将文件上传到本地文件夹下// 返回文件的访问路径String filePath = commonService.upload(SystemPathConstant.COMMON,file);return Result.success(filePath);}

配置类放行静态资源

package com.sky.config;import com.sky.constant.SystemPathConstant;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.List;/*** 配置类,注册web层相关组件*/
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {@Autowiredprivate JwtTokenAdminInterceptor jwtTokenAdminInterceptor;@Value("${file-path.disk}")private String disk;@Value("${file-path.root-path}")private String rootPath;..... 其他配置一万行/*** 设置静态资源映射* @param registry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");// Map the file directory to a URL pathregistry.addResourceHandler("/files/**").addResourceLocations("file:" +disk + ":" + "/" +rootPath + "/" +SystemPathConstant.COMMON + "/");}}

nginx映射

静态资源到本地盘符(这就你的代码中配置的判读)

  • 这个文件是开发环境的,正式环境按照正式环境的路径和盘符配置
    改完记得重启一下
       location /files/ {alias D:/sky_take_out/common/;}
  • 有的时候会访问403 异常
  • 文件权限异常
  • 访问一定要访问具体文件,不要访问到文件夹
    在这里插入图片描述

相关文章:

  • FGPA实验——触摸按键
  • 3D 模型GLTF、GLB格式文件介绍使用;FBX格式
  • Linux网络之UDP与TCP协议详解
  • 水面巡检船垃圾漂浮物检测系统源码分享
  • AI智能时代:哪款编程工具让你的工作效率翻倍?
  • 前端vuex
  • 【HarmonyOS】分页滚动文本组件
  • C++不同的头文件中各种函数的操作使用(长期更新,找到新的就补充进来)
  • IntelliJ IDEA 2024.1.4 (Ultimate Edition)找不到Add Framework Support解决方法
  • 【MySQL】基本查询
  • 侧边菜单的展开和折叠
  • 领域驱动DDD三种架构-分层架构、洋葱架构、六边形架构
  • 通过openAI的Chat Completions API实现一个支持追问的ChatGPT功能集成
  • 初试AngularJS前端框架
  • 【AI驱动 TDSQL-C Serverless 数据库技术实战营】AI赋能电商数据管理
  • 【node学习】协程
  • 5、React组件事件详解
  • Android开发 - 掌握ConstraintLayout(四)创建基本约束
  • eclipse(luna)创建web工程
  • go append函数以及写入
  • golang中接口赋值与方法集
  • Mysql5.6主从复制
  • spring + angular 实现导出excel
  • vue:响应原理
  • VuePress 静态网站生成
  • Web Storage相关
  • 复习Javascript专题(四):js中的深浅拷贝
  • 如何学习JavaEE,项目又该如何做?
  • - 语言经验 - 《c++的高性能内存管理库tcmalloc和jemalloc》
  • 从如何停掉 Promise 链说起
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​​​【收录 Hello 算法】9.4 小结
  • ​Java并发新构件之Exchanger
  • (5)STL算法之复制
  • (Redis使用系列) SpringBoot 中对应2.0.x版本的Redis配置 一
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (二)pulsar安装在独立的docker中,python测试
  • (九)c52学习之旅-定时器
  • (力扣题库)跳跃游戏II(c++)
  • (六)软件测试分工
  • (五)MySQL的备份及恢复
  • (一)为什么要选择C++
  • (译) 函数式 JS #1:简介
  • (转) ns2/nam与nam实现相关的文件
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • . Flume面试题
  • .net FrameWork简介,数组,枚举
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .NET 分布式技术比较
  • .net 后台导出excel ,word
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .考试倒计时43天!来提分啦!
  • @ohos.systemParameterEnhance系统参数接口调用:控制设备硬件(执行shell命令方式)
  • @TableLogic注解说明,以及对增删改查的影响