DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Generics in Java and Their Implementation
  • Demystifying SPF Record Limitations
  • How to LINQ Between Java and SQL With JPAStreamer
  • How To Use an Automatic Sequence Diagram Generator

Trending

  • Generics in Java and Their Implementation
  • Demystifying SPF Record Limitations
  • How to LINQ Between Java and SQL With JPAStreamer
  • How To Use an Automatic Sequence Diagram Generator

Image Storage within a J2EE Web Context

Adiel Isaacs user avatar by
Adiel Isaacs
·
Apr. 18, 14 · Interview
Like (0)
Save
Tweet
Share
4.54K Views

Join the DZone community and get the full member experience.

Join For Free

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
if(!folder.isDirectory()){
if(folder.mkdir()){
log.info("Create Folder Done - "+folder.getPath());
if(subfolder.mkdir())
log.info("Create Sub - Folder Done - "+subfolder.getPath());
}
}
if(!("").equals(fileName)){
File storeFile = new File(subfolder.getPath(),fileName);
log.info("Store File Format - "+storeFile.getName()+" Path - "+storeFile.getPath());
if(!storeFile.exists()){
try {
log.info("File Stream Store File Format - "+storeFile.getName()+"- Path - "+storeFile.getPath());
FileOutputStream fos = new FileOutputStream(storeFile);
fos.write(fileUploaded.getFileData());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return storeFile.getPath();
}else{
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.

Web application

Opinions expressed by DZone contributors are their own.

Trending

  • Generics in Java and Their Implementation
  • Demystifying SPF Record Limitations
  • How to LINQ Between Java and SQL With JPAStreamer
  • How To Use an Automatic Sequence Diagram Generator

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: