diff --git a/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java b/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java index 3c68fd2..71f7725 100644 --- a/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java +++ b/nxhs-service/src/main/java/cc/yunxi/config/WebMvcConfig.java @@ -33,6 +33,7 @@ public class WebMvcConfig implements WebMvcConfigurer { "/swagger-resources/**", "/webjars/**", "/static/**", + "/upload/**", "/doc.html" ); @@ -59,6 +60,9 @@ public class WebMvcConfig implements WebMvcConfigurer { } } + + private static final String UPLOAD_PATH = System.getProperty("user.dir") + "/upload/"; + /** * 配置静态资源访问路径 */ @@ -68,6 +72,8 @@ public class WebMvcConfig implements WebMvcConfigurer { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/","classpath:/public/"); // swagger访问配置 registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/META-INF/resources/webjars/"); + // 定义资源访问路径 + registry.addResourceHandler("/upload/**").addResourceLocations("file:" + UPLOAD_PATH); } diff --git a/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java b/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java index 5bca24a..d756883 100644 --- a/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java +++ b/nxhs-service/src/main/java/cc/yunxi/service/impl/FileServiceImpl.java @@ -10,7 +10,11 @@ import cn.hutool.core.io.FileTypeUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.unit.DataSizeUtil; +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import org.springframework.web.multipart.MultipartFile; @@ -19,6 +23,8 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.List; import java.util.Optional; @@ -32,6 +38,7 @@ import java.util.Optional; */ @Service @Validated +@Slf4j public class FileServiceImpl implements IFileService { @Value("${nxhs.upload.basePath}") @@ -48,6 +55,9 @@ public class FileServiceImpl implements IFileService { @Value("${nxhs.upload.image.maxSize}") private String imageMaxSize; + @Autowired + private Environment environment; + @Override public FileUploadRespVO uploadFile(FileUploadReqVO uploadReqVO) { return upload(uploadReqVO.getFile(), FileStyleEnum.DOC); @@ -75,7 +85,7 @@ public class FileServiceImpl implements IFileService { private FileUploadRespVO upload(MultipartFile file, FileStyleEnum fileStyleEnum) { // 获取文件源名称 String originalName = file.getOriginalFilename(); - try (InputStream inputStream = file.getInputStream()){ + try (InputStream inputStream = file.getInputStream()) { // 获取文件二进制内容 byte[] content = IoUtil.readBytes(inputStream, false); // 文件类型、大小校验 @@ -100,18 +110,31 @@ public class FileServiceImpl implements IFileService { // 判断文件是否存在,如果存在直接返回 todo // path = Optional.ofNullable(path).orElse(basePath) + File.separator; // 确定上传路径 - String URI = File.separator + fileStyleEnum.toString().toLowerCase() + File.separator + fileName; - String fullPath = basePath + URI; + String URI = basePath + fileStyleEnum.toString().toLowerCase() + "/" + fileName; + String fullPath = System.getProperty("user.dir") + URI; // 上传到文件存储位置 FileUtil.writeBytes(content, fullPath); FileUploadRespVO uploadRespVO = new FileUploadRespVO(); uploadRespVO.setName(originalName); uploadRespVO.setField(fileName); - uploadRespVO.setUrl(URI); + uploadRespVO.setUrl(getUrl(URI)); return uploadRespVO; // 入库操作 todo } catch (IOException exception) { throw new BizIllegalException("上传失败"); } } + + + // 拼接为完整的静态访问地址 + private String getUrl(String Uri) { + String port = environment.getProperty("server.port"); + String ip = "localhost"; + try { + ip = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + log.info("获取服务IP失败", e); + } + return StrUtil.format("http://{}:{}{}", ip, port, Uri); + } } diff --git a/nxhs-service/src/main/resources/application.yml b/nxhs-service/src/main/resources/application.yml index f0ad7aa..8ac8b90 100644 --- a/nxhs-service/src/main/resources/application.yml +++ b/nxhs-service/src/main/resources/application.yml @@ -73,8 +73,9 @@ nxhs: - /api/common/hsylogin - /api/common/shlogin - /api/test/** + - /api/file/download upload: - basePath: ${user.dir}/upload/ + basePath: /upload/ file: allowType: doc,docx,xls,xlsx,pdf maxSize: 100MB