【问题标题】:Headless Exception in JFilechooserJFilechooser 中的无头异常
【发布时间】:2013-04-15 05:59:05
【问题描述】:

我有一个严重的问题。问题是: java.awt.HeadlessException

现在的问题是,我在一个月前编写了相同的代码,并且在 Windows 7 和 NetBeans 7.1 中运行良好,不同之处在于他在 servlet 中编写了代码但我是在 Java 文件中编写的,然后从小服务程序。

BELIEVE ME IT'S 100% WORKS.

但现在我在 Windows 8 和 NetBeans 7.3 中只有这两个被改变。现在它不工作给出了一个无头异常。我该怎么办???
现在请告诉我如何上传文件?我需要完整的目录路径,它将保存在数据库中。
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);

上传文件.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();
    }
}


}

【问题讨论】:

  • 好吧,除非我们看到你的代码,否则我们无话可说
  • 您尝试运行的文件选择器代码将在服务器上执行,而不是在客户端机器上执行,因此您的服务器很可能在无头模式下运行(不是视频卡/视频输出)跨度>
  • 您也不太可能希望用户从您的服务器中选择文件。另请参阅Q&A
  • 解决办法是什么??它在 Windows 7 中运行。

标签: java javascript swing jsp


【解决方案1】:

您正在混合 WebApplication(HTML/JSP/Servlet/Java EE 和朋友)和 DesktopApplication(AWT/Swing/JavaFX 和朋友)的概念。虽然有一些可能的混合和匹配,但在你的情况下似乎没有任何意义。

如果您开发 Web 应用程序,使用 JFileChooser 是没有用的,因为它会在服务器端打开,而不是在客户端打开(不过,开发人员犯的一个典型错误是打开 JFileChooser 并相信它工作,因为客户端和服务器在开发时运行在同一台机器上)。

执行此操作的正确方法是在&lt;form enctype="multipart/form-data" method="post"&gt; 表单中添加&lt;input type="file" name="file"&gt;,然后从请求中检索数据。

顺便说一句,在打开JFileChooser 时,您不能传递像fc.showOpenDialog(new JPanel()) 这样的随机父组件,但您应该提供一个已经显示在Window 中的适当组件(但这与您的情况无关)。

考虑也不要像你一样有空的catch(Exception e) 块。发生这种情况时,调试起来非常困难。

【讨论】:

  • 感谢您的宝贵建议。我目前正在搜索它并发现您是正确的。它看起来像工作,但实际上没有。我正在做。 enctype="multipart/form-data" -- 这对我来说是全新的东西。如果我有任何问题,我会在这里写。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
相关资源
最近更新 更多