使用Docker上传后如何在Sping Boot 中服务静态文件?

xggvc2p6  于 2023-04-29  发布在  Docker
关注(0)|答案(1)|浏览(225)

我对接了我的应用程序,这个控制器完美地将我的图像保存在磁盘上的本地。
我的控制器:图片保存在根目录

@Slf4j
@RestController
@RequestMapping(value = "/api/v1/image")
public class ImageController {

    @PostMapping
    public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        File file = new File(UUID.randomUUID().toString() + "." + multipartFile.getOriginalFilename());
        // if file already exists will do nothing
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(multipartFile.getBytes());
        fos.close();
        return ResponseEntity.ok(file.toURI());
    }
}

在我的Docker容器中:

root@server:/APP/spring-app# docker exec -it 5e4 sh
# ls
c7fb1b0b-41f8-4265-a388-d0b5e09e563f.Снимок.JPG spring.jar
# ^C
#

inside docker container

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:/");
    }
}

但是当我试图通过链接(https://api.*****.ru/img/c7fb1b0b-41f8...23432.JPG)我看到这个错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Apr 28 09:07:15 UTC 2023
There was an unexpected error (type=Not Found, status=404).

Error
我的Dockerfile:

FROM maven:3.6.0-jdk-13-alpine AS maven

WORKDIR /app
COPY ./src ./src
COPY pom.xml .
# Compile and package the application to an executable JAR
RUN mvn package

# For Java 11,
#FROM adoptopenjdk/openjdk11
FROM adoptopenjdk/openjdk11:x86_64-ubuntu-jre-11.0.18_10

WORKDIR /opt/app

# Copy the dining-room-spring.jar from the maven stage to the /opt/app directory of the current stage.
# TODO How i can set name of the final .jar?
COPY --from=maven ./app/target/spring.jar .
EXPOSE 8080
ENTRYPOINT ["java","-jar", "spring.jar"]

我该如何解决我的问题?我只是想打开这个链接(https://api.*****.ru/img/c7fb1b0b-41f8.23432.JPG)并查看我的图像。thx帮助!

jdgnovmf

jdgnovmf1#

默认情况下,Sping Boot 只提供JAR文件内部的静态内容。Serve Static Resources with Spring
默认情况下,此处理程序提供来自类路径上的/static、/public、/resources和/META-INF/resources目录中的静态内容。
在Maven项目中,这些目录(staticpublic等))应该位于src/main/resources,e.例如src/main/resources/static
要使用外部文件夹,请设置spring.web.resources.static-locations属性,如我提供的链接所示。在你的情况下,我认为它应该是spring.web.resources.static-locations=classpath:/static,file:.。这是非常危险的,因为它也使您的应用程序本身可下载。最好使用子目录来存储和检索文件:spring.web.resources.static-locations=classpath:/static,file:files/
(Note我还包括了classpath:/static。这是为了提供捆绑到应用程序中的任何静态内容。您可以根据需要省略或替换它。)
但是,最安全的选择可能是添加一个单独的端点来检索文件。这使您可以更好地控制哪些内容可以下载,哪些内容不可以下载。

@GetMapping("{file}")
    public ResponseEntity<?> downloadFile(@PathVariable("file") String fileName) throws IOException {
        // Check that fileName does not contain any characters that allow escaping the directory
        // Then, open the file and return its contents, content type, content length, etc.
    }

相关问题