在浏览器中运行的自签名小程序无法复制文件,写入,编码等

时间:2012-11-25 03:24:34

标签: java applet

我写了一个签名的applet,它可以接收文件并将其写入本地机器文件目录。我使用NetBeans在Windows 7上工作。在Appletviewer中,所有运行正确。但是,当我在网络浏览器中使用我的网页在上下文中运行applet时,applet可以在本地目录中创建文件,但我无法使用copyFile()中的apache common.io方法。我可以写入使用bufferedWriter()创建的文件,但不能使用fileWriter。我需要使用Java库中的Encoder将文件从 .wav 转码为 .mp3 ,但它在浏览器中不起作用。 (我试过用户主文件夹和其他文件夹,但结果是一样的。)

以下是我尝试实施文件复制的代码。不会抛出任何异常,但也不会复制该文件。

 try {
   FileUtils.copyFile(source, target);
 } catch(IOException e){
   jLabel4.setText("Error: " + e.getMessage());    
 }

我可以捕获异常,所以这里是我的Java控制台中的文本,当我按下应该复制的按钮时:

 try { FileUtils.copyFile(fl1, fl2); } catch (IOException ex) { ex.printStackTrace(); jLabel4.setText("Error: " + ex.getMessage()); }

这是我的java控制台文本:

basic: Told clients applet is started
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Connecting http://localhost:8084/ with proxy=DIRECT
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar, version: null]
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Connecting http://localhost:8084/Rozhlas_jsf/lib/commons-io-2.4.jar with proxy=DIRECT
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/, version: null]
network: Cache entry not found [url: http://localhost:8084/Rozhlas_jsf/org/apache/commons/io/FileUtils.class, version: null]
network: Connecting http://localhost:8084/Rozhlas_jsf/org/apache/commons/io/FileUtils.class with proxy=DIRECT
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
    at SoundApplet.SoundApplet.jButton2ActionPerformed(SoundApplet.java:480)
    at SoundApplet.SoundApplet.access$800(SoundApplet.java:42)
    at SoundApplet.SoundApplet$11.actionPerformed(SoundApplet.java:304)
    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.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(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$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.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$1.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)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 38 more

导入的库似乎有些不好。我通过右键单击librariesfolder添加jar并添加jar。我获取了我的项目文件夹的图像,可能它可以帮助你理解我的问题,但它可以让我知道它因为我的评论数已经低了10个。

2 个答案:

答案 0 :(得分:0)

commons-io jar未包含在浏览器下载的资料中,因此其中的类不存在。你可以弄清楚如何让浏览器包含jar,或者你可以使用jarjar或shade来构建一个包含所有部分的大jar。显然,当你将commons-io添加到类路径时,netbeans没有正确设置。

答案 1 :(得分:0)

问题已解决!!

我为那些像我一样无望的人提供了一些教程

  1. 在您的webproject的属性/包装选项中添加您的applet项目。这会导致您的applet中的.jar文件在运行webproject时自动编译并复制到webroject的build / web文件夹中。

  2. 如果要为APplet使用外部库,则必须将其.jars添加到webroject的build / web文件夹中。现在,当您单击运行Webproject时,您的applet会自动编译到webroject的build / web文件夹中,您的外部库也是.jars。 (这里要注意清理你的项目,因为它会导致整个构建文件夹被删除并再次构建但没有你的外部库.jars !!当它发生时你必须再次复制.jars)

  3. 现在你必须告诉html页面加载小程序需要做什么,所以你使用这个标签

    &LT; applet codebase="http://localhost:8084/<nameOfyourWebProject>" code="<package>.<appletFileName>" archive="Applet.jar, <EXternalLibName1>.jar, <EXternalLibName2>.jar... " width="500" height="500"></applet>

  4. codebase通常是你的build / web文件夹(这里也是index.html),所以你要说Java将从这里搜索jar。代码是启动applet类的路径。存档告诉您将加载哪些罐子,因此您还需要加载applet和外部libs jar。你可以指定你想要多少罐...这就是所有,现在你可以运行你的Web项目并享受你带有外部libs jar的加载applet。但是如果你想做一些未签名的applet(what applet can and cant),你必须签署你的applet.jar(可以通过项目属性),你必须为你的applet签署所有用过的jar(用于测试posible via与java分发的keytool和jarsign utulity

    我花了很多时间来了解这些信息并成功运行我的项目,所以我希望这个小教程可以帮助你停止浪费时间:)