0

I am using Spring boot v1.5.3.RELEASE and right now, I can download files normally if I no include the extension file. When I try to download files using the typical file extensions (jpg, jpeg, mp3, mp4, etc), the Spring drops the request. An example request could be: localhost:8888/public/file/4.jpg

My Application.java is:

public class Application extends RepositoryRestMvcConfiguration {

    public static void main(String[] args) {
    //  System.getProperties().put( "server.port", 8888 );
    //  System.getProperties().put( "spring.thymeleaf.mode", "LEGACYHTML5" );
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {

        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(
                                 RepositoryRestConfiguration config) {
                config.exposeIdsFor(Noticia.class, file.class, Label.class, Reaction.class);
            }
        };

    }



}

My Controller.java piece of code is:

@RequestMapping(value = "public/file/{filename}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public FileSystemResource getPublicFileWithSuffix(@PathVariable("filename") String filename) {


        System.out.println("filename with suffix sepparated! " + filename);
        String id = filename.split(Pattern.quote("."))[0];

        file file = fileRepository.findById(Long.parseLong(id));
        File f = new File("/srv/Ressaca/locals/" + file.getId() + file.getExtension());
    return new FileSystemResource(f); 
}

After googling I have found a partial solution. With this solution, if I type some like localhost:8888/public/file/4.hello or localhost:8888/public/file/4.jpj it works, but if the extension is some real extension like (jpg,jpeg,mp3,mp4 etc) the Spring boot continues dropping the request.

Controller after googling:

@RequestMapping(value = "public/file/{filename:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody
public FileSystemResource getPublicFile(@PathVariable("filename") String filename) {


        System.out.println("filename " + filename);
        String id = filename.split(Pattern.quote("."))[0];

        file file = fileRepository.findById(Long.parseLong(id));
        File f = new File("/srv/Ressaca/locals/" + file.getId() + file.getExtension());
    return new FileSystemResource(f); 

}

How can I enable the "real files extension"?

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51
  • 2
    This might help: https://stackoverflow.com/questions/40557637/how-to-return-an-image-in-spring-boot-controller-and-serve-like-a-file-system – Shmulik Asafi Jun 28 '17 at 20:10
  • 1
    Thank you so much. I had to add `consumes = MediaType.ALL_VALUE` – Alberto Crespo Jun 28 '17 at 20:36
  • For some reason Spring takes the URL file extension into account and overrides the value from the accept header with the implied media type that comes from the extension. (At least that is what I saw debugging this problem Spring 4.3.10.RELEASE.) For example you have `.../file.txt` then it will try to match `text/plain` even if the accept header is `application/json`. Solution is to end the URL in the browser with a trainling `/`, like `.../file.txt/` – Peter Verhas Sep 09 '17 at 22:29

1 Answers1

2

Try using * instead of + but anything should work. I don't find any restriction sending extension from query parameter in any version of Spring Boot.

@RequestMapping("/public/file/{fileName:.*}")
bipinstha
  • 21
  • 3