Easier as shown below.
MyApplication.java:
public class MyApplication extends WebApplication { ... public void init() { ... final String resourceId = "images"; getSharedResources().add(resourceId, new FolderResource(new File(getServletContext().getRealPath("img")))); mountSharedResource("/image", Application.class.getName() + "/" + resourceId); } ... }
FolderResource.java:
public class FolderResource extends WebResource { private File folder; public FolderResource(File folder) { this.folder = folder; } @Override public IResourceStream getResourceStream() { String fileName = getParameters().getString("file"); File file = new File(folder, fileName); return new FileResourceStream(file); } }
And then you can get any image from the "img" folder inside your application at a simple URL:
/your-application/app/image?file=any-image.png
Here "/ your-application" is the application context path, "/ app" is the conversion of Wicket servlets to web.xml, and "any-image.png" is the name of the image file.
source share