Som*_*yal 1 javascript java swing jsp
我有一个严重的问题.问题是: java.awt.HeadlessException
现在的问题是,我在一个月之前编写了相同的代码,它在Windows 7和NetBeans 7.1中运行良好.不同之处在于他在servlet中编写代码但是我在Java文件中编写然后从servlet调用该方法.
BELIEVE ME IT'S 100% WORKS.
Run Code Online (Sandbox Code Playgroud)
但现在我在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"/>
Run Code Online (Sandbox Code Playgroud)
UploadImage.java(Servlet的):
String image="images/"+new UploadFile().Upload();
request.getRequestDispatcher("Register.jsp?image="+image).forward(request, response);
Run Code Online (Sandbox Code Playgroud)
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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)
    您正在混合WebApplication(HTML/JSP/Servlet/Java EE和朋友)和DesktopApplication(AWT/Swing/JavaFX和朋友)的概念.虽然有一些可能的混合和匹配,在你的情况下它似乎没有任何意义.
如果您开发一个Web应用程序,使用a JFileChooser是无用的,因为它将在服务器端打开,而不是客户端(虽然,开发人员的一个典型错误,是打开JFileChooser并相信它有效,因为客户端和服务器运行在开发时同一台机器).
执行此操作的正确方法是<input type="file" name="file">在表单中添加一个,<form enctype="multipart/form-data" method="post">然后从请求中检索数据.
顺便说一句,当打开a时,JFileChooser你无法传递这样的随机父组件,fc.showOpenDialog(new JPanel())但是你应该提供一个已经显示在a中的适当组件Window(但这与你的情况无关).
考虑一下也不要catch(Exception e)像你那样拥有空块.当发生这种情况时,它很难调试.
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1103 次  |  
        
|   最近记录:  |