Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Latest commit

 

History

History
29 lines (24 loc) · 1.03 KB

ServletUriComponentsBuilder.md

File metadata and controls

29 lines (24 loc) · 1.03 KB

ServletUriComponentsBuilder

Nome do arquivo na URL

String url = ServletUriComponentsBuilder
              .fromCurrentContextPath() // caminho base da aplicação (http://localhost:8080)
              .path("/download") // caminho até o recurso que gerencia os arquivos na aplicação (http://localhost:8080/download)
              .path(fileName) // adicionar o nome do arquivo no caminho atual (http://localhost:8080/download/arquivo.png)
              .toUriString(); // construção da url
  • Exemplo retirado do projeto spring-boot-multipartfile-sample

    public static FileDTO of(MultipartFile file) {
        String fileName = file.getName();
        String url = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/download")
                .path(fileName)
                .toUriString();
    
        return new FileDTO(
                fileName,
                file.getContentType(),
                url
        );
    }