0
votes

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:

enter image description here

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?

1
Simply save the files in an external directory. That way the will not be deleted, if you redeploy your apps. - Stefan
That will be awesome but how can I go about that without passing through an app? - Jevison7x
Have you tried using an absolute path, like: ´new File(/var/tomcat/data/images);´? - Stefan

1 Answers

0
votes

After doing some more research on the tomcat documentation about Contexts, I was able to deduce that the solution to the problem was to add a new Context tag to the Host tag in my tomcat/conf/server.xml file:

      <Host name="admin.zaapmart.com"  appBase="webapps/zaapmart.com/ZaapMartAdmin"
        unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
        <Alias>www.admin.zaapmart.com</Alias>
        <Context path="" reloadable="true" docBase="/home/royalsee/tomcat/webapps/zaapmart.com/ZaapMartAdmin" crossContext="true"/>
        <!-- next line of code did the trick -->
        <Context path="/ImageUploads" reloadable="true" docBase="/home/royalsee/tomcat/webapps/zaapmart.com/ImageUploads" crossContext="true"/>
      </Host>

and I modified my servlet code to this:

//...
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 uploadsContext = adminContext.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 ...
}
//...

After that I restarted the tomcat server and everything worked the way I wanted!