Spring boot 不接受文件扩展名

问题描述 投票:0回答:1

我正在使用

Spring boot v1.5.3.RELEASE
,现在,如果我不包含扩展文件,我可以正常下载文件。当我尝试使用典型的文件扩展名(jpg、jpeg、mp3、mp4 等)下载文件时,Spring 会丢弃该请求。请求示例可以是:
localhost:8888/public/file/4.jpg

我的Application.java是:

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);
            }
        };

    }



}

我的Controller.java代码是:

@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 separated! " + 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); 
}

谷歌搜索后我找到了部分解决方案。使用这个解决方案,如果我输入一些类似

localhost:8888/public/file/4.hello
localhost:8888/public/file/4.jpj
的内容,它就可以工作,但是如果扩展名是一些真正的扩展名,例如(jpg、jpeg、mp3、mp4 等),Spring boot 会继续丢弃请求。

控制器谷歌搜索后:

@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); 

}

如何启用“真实文件扩展名”?

java spring spring-mvc file-extension
1个回答
2
投票

尝试使用 * 而不是 + 但任何东西都应该有效。我在任何版本的 Spring Boot 中都没有发现任何从查询参数发送扩展的限制。

@RequestMapping("/public/file/{fileName:.*}")
© www.soinside.com 2019 - 2024. All rights reserved.