2016-09-21 214 views
0

在我的Spring Boot应用程序中,该应用程序将在本地运行,用户需要能够将照片从操作系统拖放到网页中并显示它们。在操作系统上的文件的路径可以在启动时设定:Spring Boot - 无需重新启动应用程序即可更新ResourceHandlerRegistry

@SpringBootApplication 
public class UploaderApplication extends WebMvcConfigurerAdapter { 

private String workingDir = "/Users/example/Desktop/"; 

public static void main(String[] args) { 
    SpringApplication.run(UploaderApplication.class, args); 
} 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/dir/**") 
      .addResourceLocations("file://" + workingDir) 
      .setCachePeriod(0); 
    System.out.println(workingDir); 
} 

但我需要给用户更新了文件的从应用程序运行后即将到来的目录的能力,因为绝对路径在应用程序启动时并不总是被知道。如果我通过用户输入的新工作目录路径从浏览器发送GET请求,我如何更新注册表?

谢谢。

+0

为什么不引入一个配置bean的路径? –

回答

0

这就是我决定解决它的方法。用户可以填写表单以使用他们将从其上传的目录更新数据库。当应用程序加载时,它获取目录列表并为它们创建标识符,然后可以在html中使用它们。

@SpringBootApplication 
public class boxApplication extends WebMvcConfigurerAdapter { 

@Autowired private TemplateRepository templateRepository; 

public static void main(String[] args) { 
    SpringApplication.run(boxApplication.class, args); 
} 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 

    Template template = templateRepository.findById(1); 

    for (UploadDirectory local: template.getLocalDirectories()){ 

     registry.addResourceHandler("/dir" + local.getId() + "/**") 
       .addResourceLocations("file://" + local.getDirectory()) 
       .setCachePeriod(0); 
    } 
} 
} 
相关问题