Loading a resource from a JAR or file system using getResourceAsStream

I am trying to create a simple http server.

But I have a little problem.

If my /www dir is in a .jar file compilation, all workse perfect: code:

 private static final String DEFAULT_FILES_DIR = "/www"; .... private String getURLFromHeader(String header) { //gettint url from header .... return DEFAULT_FILES_DIR + url; } .... System.out.println("Resources: " + url + "\n"); System.out.println("Result code: " + code + "\n"); 

for index.html I become: Resources: /www/index.html Result code: 200 β†’ everything works.

But when I do this:

 private static final String DEFAULT_FILES_DIR = "D:/.../.../.../www"; // absolute pass; .... .... 

the server says: "No one, I don’t know what you will do from me!" equals sign and enter: Resources: D:/.../.../.../www/index.html Result code: 404 β†’ the file was not found.

What could it be?

Ps I tried to set a private static final String DEFAULT_FILES_DIR = "D:\\...\\...\\...\\www"; but that wouldn’t work!))))

+2
java file jar resources
source share
1 answer

You use Class.getResourceAsStream() to load the resource. This uses the class loader to load the resource, and it can only load paths inside the current class path. It cannot load arbitrary local files.

What you need to do is use Class.getResourceAsStream() for resources in the class path and use the underlying FileInputStream for resources on the file system.

You will need to distinguish between the two. Exactly how you implement this depends on your requirements. You have many options, several of them:

  • Try loading with getResourceAsStream() , and if that fails, use FileInputStream . This is a bit messy, but might work for you. The caveat is that you run the risk of mistakenly downloading a local file when you want to load a resource if the path to the resource was unintentionally incorrect but matches the local file.
  • Process lines starting with "D:" as a file. It is also a bit careless and can complicate the processing of files in other places or relative file paths, but it is very simple and may work for your application.
  • Use a well-formed java.net.URL , and if the schema type is a "file", treat it as a local file.
  • Tell about this behind some resource loader interface that you build on the type of resource that you know in advance. For example (handle exceptions if you want):

     interface ResourceLoader { public InputStream getInputStream (); } class ClassResourceLoader implements ResourceLoader { private final String resource; public ClassResourceLoader (String resource) { this.resource = resource; } @Override public InputStream getInputStream () { return HttpServer.class.getResourceAsStream(resource); } } class FileResourceLoader implements ResourceLoader { private final String resource; public FileResourceLoader (String resource) { this.resource = resource; } @Override public InputStream getInputStream () { try { return new FileInputStream(resource); } catch (Exception x) { return null; } } } private ResourceLoader getResourceLoaderFromHeader (String header) { return ...; // whatever is appropriate. } 

You have many options, but take the main point: you cannot load local file resources using Class.getResourceAsStream() , and you will have to handle two cases differently, in any way convenient for you.

+7
source share

All Articles