Over a million developers have joined DZone.

Image Storage within a J2EE Web Context

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

In the big coding world, Say you have Web Application images that needs to be uploaded or even copied from one directory to another within your application container.

The Engine behind this processing is:

- Servlet Context  or getServlet() method in some Frameworks.

Using a Controller Class within  MVC pattern for processing, it is vital we get the Real Path to our Application context using this:

String webappPath = getServlet().getServletContext().getRealPath("/"); 

Once the Real Path is attained we can process the image to any created directory within the Web Context.

Please Note:

To create a Sub-Folder, the full path of the Parent Folder needs to be added into the sub-folder path.

String path = context+"\\photos";
File folder = new File(path);
File subfolder = new File(context+"\\photos\\"+businessName);
log.info("Create Folder - "+folder.getPath());
//Make the Folder
log.info("Create Folder Done - "+folder.getPath());
log.info("Create Sub - Folder Done - "+subfolder.getPath());
File storeFile = new File(subfolder.getPath(),fileName);
log.info("Store File Format - "+storeFile.getName()+" Path - "+storeFile.getPath());
try {
log.info("File Stream Store File Format - "+storeFile.getName()+"- Path - "+storeFile.getPath());
FileOutputStream fos = new FileOutputStream(storeFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
return storeFile.getPath();
return "Photo Failed to load";

 The Servlet Context can be used to copy from one directory to another.

public void copyLocalImagesToServer(String srcFolder,String destination){

String webappPath = getServlet().getServletContext().getRealPath("/");
FileUtils file = new FileUtils(); File src = new File(srcFolder); File destDir = new File(webappPath+destination); try { file.copyDirectoryToDirectory(src, destDir); log.info("Copy Dir "+src.getPath()+" to Src - "+destDir.getPath()); } catch (IOException e) { // TODO Auto-generated catch block log.info("Image Copy Failure from "+src.getPath()+" to "+destDir.getPath()); e.printStackTrace(); } }

Remember various frameworks uses different Context methods.

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}