How to insert a network file using Spring?

How can I tell the Spring

resource to be a network resource and let it run automatically?

@Value("\\MY-MACHINE\thefile.txt")
private Resource file;

      

Result:

Caused by: java.io.FileNotFoundException: class path resource [MY-MACHINE\thefile.txt] cannot be resolved to URL because it does not exist

      

+3


source to share


4 answers


I am assuming your application is deployed on Windows. I don't have a Windows workstation to test it, but as far as I can remember this is:

  • create a symbolic link to your network folder:

    mklink /D C:\my-machine \\MY-MACHINE\
    
          

  • reference this symlink with an absolute path:



@Value("file:///C:/my-machine/thefile.txt")
private Resource file;

      

+1


source


Nobody seems to know about this, I found that I just need to use double slashes, for example:

@Value("\\\\MY-MACHINE\\thefile.txt")
private Resource file;

      



In some cases, you may need the instruction file:

earlier, for example: file:\\\\MY-MACHINE\\thefile.txt"

+1


source


This is the easiest way using classpath

import org.springframework.core.io.Resource;

@Value("classpath:<path to file>")
private Resource file;

      

0


source


Accessing a file on a Windows share will not work directly. See connecting to a shared folder in windows with java for more information on how to do this.

0


source







All Articles