1
resposta

Exibindo arquivos com espaços no nome

É importante fazer decode da URI antes de obter o nome do arquivo - URLDecoder.decode. Segue como fica a função do Servlet:

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String relativePathStr = URLDecoder.decode(req.getRequestURI(), "UTF-8").split("/file/")[1];

        String fullPathStr = FileSaver.getPath(relativePathStr);

        Path sourcePath = Paths.get(fullPathStr);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentType = fileNameMap.getContentTypeFor("file:" + sourcePath);

        res.reset();
        res.setContentType(contentType);
        res.setHeader("Content-Length", String.valueOf(Files.size(sourcePath)));
        res.setHeader("Content-Disposition", String.format("\"%s\"", sourcePath.getFileName()));

        FileSaver.transfer(fullPathStr, res.getOutputStream());
    }
1 resposta

Oi Felipe, tudo bem?

Obrigado por compartilhar (=

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software