Spring [SOLVED]: how to access local drive file from server link

Spring [SOLVED]: how to access local drive file from server link

Home Forums Frameworks Spring Spring [SOLVED]: how to access local drive file from server link

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #243799

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am working on spring boot trying to save the image on local drive and store the link of image in database but i am facing the problem to access the image through link.

    The files is stored on F:ImageDataReportImage20171218 how can allow the access image in local drive using link like localhost:8085/ImageData/ReportImage/20171218/sample.jpg

    i have also try setting server.servlet-path=F:ImageData and server.context-path=F:ImageData in application.property but problem not solve.

    also if i save image in src/main/resources/static/sample.jpg can easily access but i want access from some local drive.

    please someone help me to solve my problem.

    Thank you.

    #243800

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You could try to add resource handlers to spring configuration:

    @Configuration
    @EnableWebMvc
    public class MvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
              .addResourceHandler("/images/**")
              .addResourceLocations(new File("F:ImageData").toURI().toString()); 
        }
    }
    

    And with this configurations you will be able get images by url http://<host>/images/<path> which will try to get image from your disk by path F:ImageData<path>

    More info about this possibility you could find here

    Source: https://stackoverflow.com/questions/47866866/how-to-access-local-drive-file-from-server-link
    Author: kurt
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.