如下所示。
我的应用程序.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);
}
...
}
文件夹资源.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);
}
}
然后您可以通过简单的 URL 从应用程序内的“img”文件夹中获取任何图像:
/your-application/app/image?file=any-image.png
这里“/your-application”是应用程序上下文路径,“/app”是web.xml中的Wicket servlet映射,“any-image.png”是图像文件的名称。