1

I have a Serious problem. Question is: java.awt.HeadlessException

Now the problem is, I wrote the same code before a month and it worked fine in Windows 7 & NetBeans 7.1 Difference is that he wrote code in a servlet But i was write the in a Java file & then call the method from servlet.

BELIEVE ME IT'S 100% WORKS.

But now i am in Windows 8 & NetBeans 7.3 Only this two is changed.Now it is not working gives a Headless Exception. What can i do???
Now please tell me how to upload a file? I need full directory path, It will save in database.
Register.jsp:

<a href="UploadUserImage">
  <input type="button" class="button round blue image-right ic-upload text-upper" value="Upload" name="upload"/>
</a>
<font style="font-family: Times New Roman; font-size: 16px; color: #2a2e36; font-style: italic;"><%= image %></font>
<input type="hidden" value="images/Member/<%= image %>" name="image"/>

UploadImage.java(Servlet):

String image="images/"+new UploadFile().Upload();
request.getRequestDispatcher("Register.jsp?image="+image).forward(request, response);

UploadFile.java:

public class UploadFile
{
File file;
public String Upload()
{
    try 
    {

    final JFileChooser fc = new JFileChooser();
    String[] extensions={"jpg", "png", "gif"};
    FileNameExtensionFilter filter=new FileNameExtensionFilter("Images", extensions);
    fc.setFileFilter(filter);
    fc.setMultiSelectionEnabled(false);
    //fc.setCurrentDirectory(new File("C:\\tmp"));
    fc.setApproveButtonText("Upload");
    int retVal = fc.showOpenDialog(new JPanel());
    file=fc.getSelectedFile();
    String src,dst;
    src=file.getAbsolutePath();
    dst="C:\\Users\\SHUVAM KAYAL\\Documents\\NetBeansProjects\\BookShopManagment\\BookShopManagment-war\\web\\images\\Member\\"+file.getName();
    copy(new File(src), new File(dst));
    } 
    catch (IOException ex) 
    {}  
    catch(NullPointerException e)
    {}

    return file.getName();
}
public void copy(File sourceLocation , File targetLocation) throws IOException {
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i=0; i<children.length; i++) {
            copy(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    } else {

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}


}
Community
  • 1
  • 1
Somnath Kayal
  • 393
  • 2
  • 9
  • 25
  • 1
    well well unless we see your code, we can not say anything – Satya Apr 22 '13 at 10:53
  • 1
    The file chooser code you are trying to run, will execute on the server, not on the clients machine, so it's likey your server is running in headless mode (not video card/video out) – MadProgrammer Apr 22 '13 at 11:08
  • It's also unlikely that you want the user to choose a file from your server. See also this [Q&A](http://stackoverflow.com/q/2048026/230513). – trashgod Apr 22 '13 at 11:10
  • what is the solution?? It was running in windows 7. – Somnath Kayal Apr 22 '13 at 11:11

1 Answers1

6

You are mixing concepts of a WebApplication (HTML/JSP/Servlet/Java EE and friends) and DesktopApplication (AWT/Swing/JavaFX and friends). While there are some possible mix & match, in your case it does not seem to make any sense.

If you develop a web application, using a JFileChooser is useless since it will open on the server side, not the client one (though, a typical mistake made by developers, is to open a JFileChooser and believe it works, because the client and server run on the same machine when developping).

The proper way to do this, is to add an <input type="file" name="file">, within a form <form enctype="multipart/form-data" method="post"> and then retrieve the data from the request.

By the way, when opening a JFileChooser you cannot pass a random parent component like this fc.showOpenDialog(new JPanel()) but you should provide an appropriate component which is already displayed within a Window (but that is not relevant to your case).

Consider also never having empty catch(Exception e) blocks like you do. When this happens, it makes it really hard to debug.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Thank you for your valuable Suggestion. I am searching about it currently & found you are correct. It's looks like working but actually not. I am working on it. enctype="multipart/form-data" -- It is Completely new thing to me.If i have any problem I write it here. Thank you. – Somnath Kayal Apr 22 '13 at 11:18