org.javawebstack.httpserver.util.DirectoryFileProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-server Show documentation
Show all versions of http-server Show documentation
This library provides a routing and request mapping stack on top of the well known and industry proven eclipse jetty http server. It also supports websockets.
The newest version!
package org.javawebstack.httpserver.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class DirectoryFileProvider implements FileProvider {
private final File directory;
public DirectoryFileProvider(File directory) {
this.directory = directory;
}
public InputStream getFile(String path) {
File file = new File(directory, path);
if (!file.exists() || !file.isFile() || !isInside(directory, file))
return null;
try {
return new FileInputStream(file);
} catch (FileNotFoundException ignored) {
}
return null;
}
private static boolean isInside(File directory, File file) {
if (file.getParentFile().equals(directory))
return true;
if (directory.getParentFile() == null)
return false;
return isInside(directory.getParentFile(), file);
}
}