Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6004051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:12:30+00:00 2026-05-23T01:12:30+00:00

I have this situation: I am trying to remove an old avatar image for

  • 0

I have this situation: I am trying to remove an old avatar image for a user before putting a new one from the managed bean.

String fileName = "resources/img/useravatars/" + getSessionBean().getSearchAccount().getAvatar();
File f = new File(fileName);

I’ve googled a bit and it seems that I can get a path to that folder from ExternalContext like:

FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext(). ...

But I couldn’t find an appropriate method from class docs. Could you please help with what to put instead of … or suggest a better solution.

PS. Somehow, I suspect it is possible to hardcode the link, but no luck so far.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:12:30+00:00Added an answer on May 23, 2026 at 1:12 am

    I understand that the file is embedded in the WAR and that you’re looking for the ExternalContext#getRealPath() method to resolve it based on a web-relative path. As per the Javadoc, this method is introduced in JSF 2.0 and does not exist in JSF 1.x. You seem to be using JSF 1.x, otherwise you wouldn’t have asked this question. You need to use ServletContext#getRealPath() instead (which is also what the new JSF 2.0 method is delegating to, under the covers).

    String relativeWebPath = "/resources/img/useravatars/" + ...;
    ServletContext servletContext = (ServletContext) externalContext.getContext();
    String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath);
    // ...
    

    However, there’s a big BUT: you can and should not write to the expanded WAR. Deleting files is also writing. Whenever you redeploy the WAR or restart the server, every change will be reverted and the expanded WAR will retain its initial state, hereby losing all changes made in the expanded WAR since the last deploy.

    You really need to store those files in an external location whose root location can then be hardcoded or definied in some external configuration (properties) file. This way you can use java.io.File stuff the usual way.

    There are several ways to serve files from an external location. You can find them all in the answer of the following question: Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know questions of this kind have been asked before, but my situation differs
I have run into a situation where I need to manually remove old dialogs
Strange situation: I am trying to remove some hard coding from my code. There
I have a situation where I want to associate multiple values from one table
So, I have this situation where I need to see if an object is
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,
The situation is this: You have a Hibernate context with an object graph that
This is often situation, but here is latest example: Companies have various contact data
I have somehow misconfigured fingers. This leads to a very annoying situation. I select
Here is the situation. This small company I'm working with wants to have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.