3

How can I tell Spring that the resource is supposed to be a network resource, and let it autowire accordingly?

@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
membersound
  • 81,582
  • 193
  • 585
  • 1,120

4 Answers4

1

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

  • create symbolic link to your network location:

    mklink /D C:\my-machine \\MY-MACHINE\
    
  • refer to this symlink with absolute path:

@Value("file:///C:/my-machine/thefile.txt")
private Resource file;
Maciej Walkowiak
  • 12,372
  • 59
  • 63
  • This works great for local file, but how is the syntax for network fileS? – membersound Dec 23 '14 at 10:15
  • You can create symlink to this network location, so that file is visible as a local file. In Unix it's 'ln -s' in Windows take a look for examaple here: http://superuser.com/questions/210824/creating-a-symbolic-link-to-mapped-network-drive-in-windows – Maciej Walkowiak Dec 23 '14 at 10:35
1

As nobody seems to be aware of, I found out that it's just necessairy to use double slashes like:

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

In some cases you may required a file: statement before, like: file:\\\\MY-MACHINE\\thefile.txt"

membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

This is the easiest way using classpath

import org.springframework.core.io.Resource;

@Value("classpath:<path to file>")
private Resource file;
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • a network file is probably not on the classpath (hopefully, that would be nasty...) – eis Jan 19 '15 at 08:31
0

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

Community
  • 1
  • 1
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211