【问题标题】:FileUtils.copyURLToFile not working for project fileFileUtils.copyURLToFile 不适用于项目文件
【发布时间】:2016-10-03 04:06:31
【问题描述】:

我正在构建 Java SWT 应用程序。作为整个过程的一部分,我正在处理一个具有特定格式来处理事物的 excel 文件。我想要的是我的 JFrame 上的链接,该链接触发下载存储在我的项目中的示例 excel 文件,而无需本地或互联网依赖。我不知道我怎么能做到这一点。

我试过这个 FileUtils.copyURLToFile 但它抛出空指针异常。我正在显示我的代码。任何帮助将不胜感激。

public void actionPerformed(ActionEvent arg0) {
            log("clicked to download");
            String temp_path = System.getProperty("user.dir")+"\\resources\\EmailSheet.xlsx";
            log("path = " + temp_path);
            URL inputUrl = getClass().getResource(temp_path);
            log("URL = " +inputUrl.toString());
            File dest = new File("D:/new_file.xlsx");
            try {
                FileUtils.copyURLToFile(inputUrl, dest);
            } catch (IOException e) {
                log("Error saving sample file : " + e.getMessage().toString());
                e.printStackTrace();
            }
        }

我的控制台正在打印此错误

  Mon Oct 03 09:14:29 IST 2016 : clicked to download
  Exception in thread "AWT-EventQueue-0" Mon Oct 03 09:14:29 IST 2016 : path = \resources\EmailSheet.xlsx
  java.lang.NullPointerException
at tech.excelemail.com.TechExcelEmailApp$7.actionPerformed(TechExcelEmailApp.java:221)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我想知道我的代码是否有错误。或者如果我走错了路。如果您认为缺少未指定的内容,请问我任何事情。

提前致谢。

【问题讨论】:

标签: java file nullpointerexception embedded-resource java-io


【解决方案1】:

很难相信System.getProperty("user.dir") 真的返回了一个空字符串,正如您的跟踪所暗示的那样。这是真实的代码吗?

在任何情况下,资源都是相对于 CLASSPATH 而不是文件系统命名的,因此user.dir 与它无关。它们不是文件。他们不使用反斜杠。使用正斜杠。并确保命名的资源在 JAR 文件中以该名称存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 2015-12-07
    • 1970-01-01
    • 2021-11-25
    • 2017-10-13
    • 2019-03-16
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多