The main interface to be aware of is Resource which defines several self-explanatory methods. When using this mechanism all you need to do is supply a URI. The usual formats of file:, http: are supported together with the familiar Spring specific classpath: protocol.
The following Spring classes implement the Resource interface and these are:
- UrlResource
- ClassPathResource
- FileSystemResource
- ServletContextResource
- InputstreamResource
- ByteArrayResource
The code below demonstrates how to use this mechanism to read a file on the file system and classpath and to read data from a URL.
public class Example3_Application_Resource {
public static void main(String[] args) {
try {
/**
* Get hold of an application context, it doesn't matter which file in this case...
*/
ApplicationContext ctx = new FileSystemXmlApplicationContext(
Constants.PATH_TO_RESOURCES + "example3_ApplicationContext_Events.xml");
demoFileResource(ctx);
demoClasspathResource(ctx);
demoURLResource(ctx);
} catch (IOException e) {
System.out.println("IO error: " + e.getMessage());
}
}
private static void demoFileResource(ApplicationContext ctx) throws IOException {
String fileName = "file:///tmp/example3_ApplicationContext_Events.xml";
Resource fileResource = loadResource(ctx, fileName);
File file = fileResource.getFile();
System.out.println("File is: " + file.getAbsolutePath());
displayURIInfo(fileResource);
readResource(fileResource, "This is the file: ");
}
private static Resource loadResource(ApplicationContext ctx, String resourceName) {
Resource resource = ctx.getResource(resourceName);
System.out.println("The resource name is: " + resource.getFilename());
return resource;
}
private static void displayURIInfo(Resource resource) throws IOException {
URI url = resource.getURI();
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("Path: " + url.getPath());
}
private static void readResource(Resource resource, String message) throws IOException {
if (testResource(resource)) {
InputStream inp = resource.getInputStream();
printResource(inp, message);
}
}
private static void printResource(InputStream inp, String msg) throws IOException {
System.out.println(msg);
byte[] buffer = new byte[8192];
int len;
while ((len = inp.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, len));
}
}
public static boolean testResource(Resource resource) {
boolean retVal = true;
if (!resource.exists()) {
System.out.println("resource does not exist: " + resource.getFilename());
retVal = false;
}
return retVal;
}
private static void demoClasspathResource(ApplicationContext ctx) throws IOException {
Resource classpathResource = loadResource(ctx,
"classpath:example3_ApplicationContext_Events.xml");
displayURIInfo(classpathResource);
readResource(classpathResource, "This is the classpath file: ");
}
private static void demoURLResource(ApplicationContext ctx) throws IOException {
Resource urlResource = loadResource(ctx, "http://www.google.co.uk");
displayURIInfo(urlResource);
/**
* This doesn't work if the source is a URL File file = urlResource.getFile();
* file.getAbsolutePath());
*/
readResource(urlResource, "This is the URL: ");
}
}
This example requires the following JAR files on the classpath:
- spring-context.jar - for the ApplicationContext stuff.
- spring-core.jar - for the FileSystemResource class
No comments:
Post a comment