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

ops/2024/10/25 8:17:08/

服务类

  • 系统盘符: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 异常
  • 文件权限异常
  • 访问一定要访问具体文件,不要访问到文件夹
    在这里插入图片描述

http://www.ppmy.cn/ops/117933.html

相关文章

NAT和代理服务

文章目录 NAT和代理服务1、NAT技术背景2、NAT 技术 IP 转换过程3、NAPT4、NAT 技术的缺陷5、代理服务器5.1、正向代理5.1.1、概念5.1.2、工作原理5.1.3、功能特点5.1.4、应用场景 5.2、反向代理5.2.1、概念5.2.2、工作原理5.2.3、应用场景 6、NAT 和代理服务器 NAT和代理服务 …

18.2 k8s-apiserver监控源码解读

本节重点介绍 : k8s代码库和模块地址 下载 apiserver源码 apiserver中监控源码阅读 k8s源码地址分布 k8s代码库 访问github上k8s仓库&#xff0c;readme中给出了k8s 模块的代码地址举例图片 组件仓库列表 地址 Repositories currently staged here: k8s.io/apik8s.io/a…

List几种遍历方法速度

准备数据 ArrayList<Integer> list new ArrayList<Integer>();for (int i 0; i < 100000000; i) {list.add(i); }1.for循环 for (int i 0; i < list.size(); i) {Integer str list.get(i); }2.for-each循环 for (Integer vo:list) {}3.Lambda list.forEa…

Elasticsearch黑窗口启动乱码问题解决方案

问题描述 elasticsearch启动后有乱码现象 解决方案&#xff1a; 提示&#xff1a;这里填写该问题的具体解决方案&#xff1a; 到 \config 文件下找到 jvm.options 文件 打开后 在文件末尾空白处 添加 -Dfile.encodingGBK 保存后重启即可。

前端编码规范

1、开发规范 1.1、新增 view 在 /views (opens new window)文件下 创建对应的文件夹&#xff0c;一般性一个路由对应一个文件&#xff0c; 该模块下的功能就建议在本文件夹下创建一个新文件夹&#xff0c;各个功能模块维护自己的utils或components组件。 1.2、新增 api 在 /…

Redis中BigKey与MoreKey优化笔记

1.MoreKey 在Redis中&#xff0c;MoreKey问题通常指的是当数据库中的key数量非常多时&#xff0c;使用如KEYS *这样的命令去检索所有的key&#xff0c;这会导致Redis服务阻塞&#xff0c;影响正常业务。因为Redis是单线程操作的&#xff0c;执行这类命令时会占用大量时间&…

zookeeper面试题

1. 什么是zookeeper zookeeper是一个开源的 分布式协调服务。他是一个为分布式应用提供一致性服务的软件&#xff0c;分布式应用程序可以基于Zookeeper实现诸如数据发布/订阅、负载均衡、命名服务、分布式协调/通知、集群管理、Master选举、分布式锁和分布式队列等功能。 Zooke…

LeetCode 136. 只出现一次的数字

LeetCode 136. 只出现一次的数字 给你一个 非空 整数数组 nums &#xff0c;除了某个元素只出现一次以外&#xff0c;其余每个元素均出现两次。找出那个只出现了一次的元素。 你必须设计并实现线性时间复杂度的算法来解决此问题&#xff0c;且该算法只使用常量额外空间。 示例 …