I have 3 web apps running on the same tomcat instance, one app (ZaapMartAdmin) is for the admin to upload files to another app's (ImageUploads) directory, while the third app (ROOT) is simply to read the files uploaded to (ImageUploads) and display the image files from there. I got some ideas from here, here, here, and here.
Here's the relevant code in my servlet (Please note: servlet is running on ZaapMartAdmin):
//...
String fileName = generateFileName(request);//Generate the image file name
byte[] imageBytes = getByteArray(request);//Generate the image bytes
//Where to save the image part
ServletContext adminContext = request.getServletContext();//ZaapMartAdmin context
ServletContext rootContext = adminContext.getContext("/");//ROOT context
ServletContext uploadsContext = rootContext.getContext("/ImageUploads");//ImageUploads context
String absolutePath = uploadsContext.getRealPath("");
File imagesDirectory = new File(absolutePath + File.separator + "images");
if(!imagesDirectory.exists())
imagesDirectory.mkdir();
try(FileOutputStream fos = new FileOutputStream(absolutePath + File.separator + "images" + File.separator + fileName);)
{
fos.write(imageBytes);//<-- store the image in the directory
//... store file name to database ...
}
//...
From the server end my directory structure looks like this:
The problem now is, when I run this servlet, the files will be saved inside the "ZaapMartAdmin" directory instead of the "ImageUploads" directory. And it doesn't throw any Exceptions.
Also I have added crossContext="true" in the context.xml of each of the apps.
Please what I'm I doing wrong here?
