当前位置: 首页 > 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 异常
  • 文件权限异常
  • 访问一定要访问具体文件,不要访问到文件夹
    在这里插入图片描述
http://www.lryc.cn/news/447341.html

相关文章:

  • 数学建模练习小题目
  • 不可错过的10款文件加密软件,企业电脑加密文件哪个软件好用
  • 常用卫星学习
  • 音视频入门基础:FLV专题(3)——FLV header简介
  • python中数据处理库,机器学习库以及自动化与爬虫
  • 2024最新测评:低代码平台在企业复杂应用场景的适用性如何?
  • URL中 / 作为字符串,而不是路径。
  • el-input只能输入指定范围的数字
  • 数据结构编程实践20讲(Python版)—01数组
  • 数据库实验2—1
  • 现代前端框架实战指南:React、Vue.js、Angular核心概念与应用
  • MySQL --用户管理
  • 详解前驱图与PV操作
  • 孩子来加拿大上学真的那么轻松吗?(上)
  • 【算法篇】二叉树类(1)(笔记)
  • 《C++无锁编程:解锁高性能并发的新境界》
  • 系统架构设计师教程 第9章 9.5 软件可靠性测试 笔记
  • 如何使用ssm实现校园体育赛事管理系统的设计与实现+vue
  • CSS 中的文本相关属性(line - height、font、letter - 属性、text - 属性)
  • mobaxterm、vscode通过跳板机连接服务器
  • 鸿萌数据恢复:iPhone 手机被盗后应采取哪些措施?警惕这些骗局
  • 为了学习Python熬夜部署了Jupyter Notebook 6.x
  • docker-文件复制(docker cp:用于在Docker主机和容器之间拷贝文件或目录)
  • guava里常用功能
  • su 命令:一键切换用户身份、提高su命令安全性的建议
  • 观察者模式(发布-订阅模式)
  • 耦合微带线单元的网络参量和等效电路公式推导
  • elasticsearch的Ingest Attachment插件的使用总结
  • SemiDrive E3 MCAL 开发系列(4) – Gpt 模块的使用
  • 前端导出页面PDF