how to save a base64 as image in java server faces project

I am doing a project for school. Subject of project is tshirt design. I am using jsf and primefaces. But I don't know well jsf and primefaces. I wanted save a base64 from html as image in jsf project. But when I had tried to following functions, NullPointerException. This function is called in a Servlet. A base64 is grabbed by this Servlet.

public static void save(String dataURL){

line85: ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
    ServletContext servletContext = (ServletContext) external.getContext();
    String filename = servletContext.getRealPath("cloud.png");
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] decodedBytes;
    try {

        decodedBytes = decoder.decodeBuffer(dataURL.split("data:image/(png|jpg);base64,")[1]);
        BufferedImage ByteArrayInputStream(decodedBytes));
        ImageIO.write(imag, "png", new File(filename));
    } catch (IOException e) {
        // TODO Auto-generated catch block


    23.Ara.2012 17:48:20 org.apache.catalina.core.StandardWrapperValve invoke
     SEVERE: Servlet.service() for servlet [org.soft.tshirt.servlets.DesignServlet] in context              with path [/Tshirt] threw exception
at org.soft.tshirt.servlets.DesignServlet.processRequest(
at org.soft.tshirt.servlets.DesignServlet.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.valves.AccessLogValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.AbstractHttp11Processor.process(
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
at java.util.concurrent.ThreadPoolExecutor$


The current instance of the FacesContext is only available in the HTTP request thread which is been served through the FacesServlet who is the one responsible for creating the FacesContext. In other words, only when the request URL matches the URL pattern of the FacesServlet. In other words, only JSF pages, JSF managed beans (and thus not backing beans which you instantiate yourself like as is happening here!) and all other JSF artifacts like event/phase listeners, UI components, etc have the current instance of the FacesContext available to them.

In an arbitrary homegrown HTTP servlet, the FacesContext isn't available at all, for the simple reason that the FacesServlet is not been invoked beforehand. So the getCurrentInstance() method on it would return null. You know, trying to access/invoke any fields/method on null will cause NullPointerException. See also its javadoc.

In order to achieve the sole goal of obtaining the ServletContext, just invoke the inherited GenericServlet#getServletContext() method inside the servlet.

protected void doPost(...) {
    String filename = getServletContext().getRealPath("cloud.png");
    // ...

Pass that information instead to the method responsible for creating the file. You might want to extract that code into a different class which is reused between your servlet and backing bean.

Unrelated to the concrete problem, writing files to the expanded WAR folder is really a bad practice for the reasons mentioned in among others this answer: Uploaded image only available after refreshing the page.

Need Your Help

Dynamic user control over variables (embedded language?)

c# user-interface embedded-language user-friendly flexibility

I'm creating a piece of software (written in C#, will be a windows application) and I ran into this problem-

Force table update for Kohana (even if nothing to save)

mysql timestamp kohana

I have TIMESTAMP field in database which updates with CURRENT_TIMESTAMP when table is updated. I have a form which may be updated with no changes. Problem is that I need to update TIMESTAMP ever...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.