简体   繁体   中英

Spring boot doesnt accept file extensions

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"?

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:.*}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM