Roledenez Roledenez - 1 year ago 188
Apache Configuration Question

How to upload an image to web resource folder in tomcat or glassfish

I tried to upload an image to tomcat and glassfish servers, Path I tried to set is as bellow

System.getProperty("user.dir")+ File.separator+"images"+File.separator;

It takes
as tomcat
and glassfish 'C:\Apache\glassfish4\glassfish\domains\domain1\config` directory. I am using Intellij to develop the system.

I want to upload an image to
folder so that I can access the images using
, how to do this? I have spent days of time googling but couldn't able to find useful thing

enter image description here

Answer Source

You can get the root of your deployed application with

String root = getServletContext().getRealPath("/");

This will be the equivalent of the resources directory of your source code. Add the filepath of where you want your image to be. i.e.

String filePath = root + "images/msg.jpg";

Then you can created your writer from that path

BufferedWriter writer = new BufferedWriter(new FileWriter(filepath);
writer.write(objectToWrite);//or similar

You can then access the resulting file with

getServletContext().getResource(filePath); // as URL or
getServletContext().getResourceAsStream(filePath); // as InputStream

-- Tested and worked on Payara (an application server derived from Glassfish)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download