java open file from classpath with a space charact

2019-09-05 12:49发布

I have a webapp hosted in tomcat, and a jenkins build server. I've previously read that the best way to read a file bundled with the jar, is to use the classpath rather than a hardcoded fullpath.

I usually see a variant of this to be the recommended way:

this.getClass().getResourceAsStream("/testfile.txt");

And this seems to work locally, but on the jenkins linux build server, which is setup to build in a directory containing a space, the getResourceAsStream() fail because internally it fails to lookup the file, since the path contains contains a space, which gets translated to %20.

Thus, I see only 2 possibilities, either:

  • Everyone suggesting to use the getResourceAsStream() are wrong, and that a robust use which handles paths with spaces in it, need to do something more before calling getResourceAsStream()
  • The getResourceAsStream contains a bug

I guess its really the first option, so then my question is, what's the most correct way to handle this?

0条回答
登录 后发表回答