1

我正在为下个月的考试开发一个小型应用程序。在此应用程序中,用户可以上传与其名称匹配的文件。

不幸的是,在丹麦,我们有一些外来字符(ÆØÅæøå),我的代码不会打开包含这些的文件。

有没有一种解决方法可以让我使用这些文件名?

这是打开文件的代码:

Desktop.getDesktop().open(new File("kontrakt/" + getStuderende().getKontrakt()));

还有一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: kontrakt/5_Bjørnen-Bjørn-kontrakt.docx doesn't exist.
at java.awt.Desktop.checkFileValidation(Desktop.java:209)
at java.awt.Desktop.open(Desktop.java:270)
at View.StuderendePanel.visKontraktBtnActionPerformed(StuderendePanel.java:382)
at View.StuderendePanel.access$1000(StuderendePanel.java:29)
at View.StuderendePanel$12.actionPerformed(StuderendePanel.java:284)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
4

1 回答 1

1

该代码在 Windows 中运行。但是,本主题中使用的 OSX 操作系统似乎不支持这一点。

这工作正常

package others;

    import java.awt.Desktop;
    import java.io.File;

    public class A {

        public static void main(String[] args) throws Exception{


            Desktop.getDesktop().open(new File("c://5_Bjørnen-Bjørn-kontrakt.docx"));

        }

    }

尝试打印你从getStuderende().getKontrakt()方法中得到的东西

检查它是否与您正在打开的文件相同。

于 2013-05-27T09:27:27.177 回答