SWT-App 从可运行的 .jar 或外部文件夹加载 .swf 文件

发布于 2024-12-03 02:06:20 字数 498 浏览 0 评论 0原文

我正在 eclipse 中使用 SWT Widget Library for Java 进行编程,并且正在设计一个可运行的 Java 应用程序。我已经关闭了应用程序,我只是不知道如何从“所有”计算机上的文件夹加载外部 .swf 文件。我可以从任何计算机加载图像,因为我使用 getResourceAsStream 代码行。但是“import com.docuverse.swt.flash.FlashPlayer”“loadMovie(arg, arg)”只需要一个字符串。

所以我做了 ClassName.class.getResource("blah.swf").getPath();它给了你一个字符串,我设置了它,并在 eclipse 上运行它,它可以完美地找到包中的文件。当我导出它时,我制作的可运行 Jar 找不到 .jar 文件内的“blah.swf”。

所以我的问题是,如何从 .jar 内或从外部文件夹加载 .swf 文件,以便客户端可以与 .jar 可执行应用程序一起下载,以便它可以指向这些 swf 文件。

谢谢。

I'm programming using the SWT Widget Library for Java in eclipse, and I'm designing a runnable Java application. I've got the application down, I just don't know how to load external .swf files from a folder on "ALL" computers. I can load Images from any computer, because I use the getResourceAsStream line of code. But the "import com.docuverse.swt.flash.FlashPlayer" "loadMovie(arg, arg)" only takes a String.

So I did ClassName.class.getResource("blah.swf").getPath(); which gives you a string, I set it up, and running it on eclipse it can perfectly find the file in the package. When I export it, the runnable Jar I made cannot find the "blah.swf" inside of the .jar file.

So there is my problem, how do I load my .swf files from within the .jar or from an external folder so clients can download along side the .jar executable application, so it can point to those swf files.

Thankyou.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-12-10 02:06:20

这是我在评论中讨论的方式(创建临时文件并将 flash 动画从 jar 保存到其中),

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.docuverse.swt.flash.FlashPlayer;

public class SWTFlashPlayer {
    private FlashPlayer player = null;

    private final String FLASH_FILE_PATH = "/EdnCateDance.swf";
    private final String TMP_FILE_PREFFIX = "tmp_";
    private final String TMP_FILE_SUFFIX = ".swf";

    private File swfFile = null;

    public SWTFlashPlayer() {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        try {
            swfFile = copyFileFromJar(FLASH_FILE_PATH, TMP_FILE_PREFFIX, TMP_FILE_SUFFIX);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        player = new FlashPlayer(shell, SWT.NONE);
        player.loadMovie(0, swfFile.getAbsolutePath());
        player.setSize(150, 150);
        player.activate();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    /**
     * Copy file packed inside current jar to temp file
     * @param jarPath String - path inside jar
     * @param filePreffix String - temp file preffix
     * @param fileSuffix - temp file suffix
     * @throws IOException - temp file cannot be created or writing somehow fails
     */
    private File copyFileFromJar(String jarPath, String filePreffix, String fileSuffix) throws IOException {
        File toFile = File.createTempFile(filePreffix, fileSuffix);
        // delete file after application finishes
        toFile.deleteOnExit();

        if(!toFile.canWrite()) throw new IOException("File (" + toFile.getPath() + ") not exists or is not writable!");

        FileOutputStream fos = new FileOutputStream(toFile);
        InputStream is = this.getClass().getResourceAsStream(jarPath);

        if(is == null) throw new IOException("File on jar path could not be located or loaded!");

        int read = 0;
        byte bytes[] = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            fos.write(bytes, 0, read);
        }

        fos.flush();
        fos.close();

        return toFile;
    }

    public static void main(String[] args) {
        new SWTFlashPlayer();
    }
}

我使用了 EdnCateDance.swf flash 文件,它是 SWT Flash 库中的示例之一。

Here is the way I talked about in comments (creating temp file and save flash animation from jar to it)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.docuverse.swt.flash.FlashPlayer;

public class SWTFlashPlayer {
    private FlashPlayer player = null;

    private final String FLASH_FILE_PATH = "/EdnCateDance.swf";
    private final String TMP_FILE_PREFFIX = "tmp_";
    private final String TMP_FILE_SUFFIX = ".swf";

    private File swfFile = null;

    public SWTFlashPlayer() {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());

        try {
            swfFile = copyFileFromJar(FLASH_FILE_PATH, TMP_FILE_PREFFIX, TMP_FILE_SUFFIX);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        player = new FlashPlayer(shell, SWT.NONE);
        player.loadMovie(0, swfFile.getAbsolutePath());
        player.setSize(150, 150);
        player.activate();

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

    /**
     * Copy file packed inside current jar to temp file
     * @param jarPath String - path inside jar
     * @param filePreffix String - temp file preffix
     * @param fileSuffix - temp file suffix
     * @throws IOException - temp file cannot be created or writing somehow fails
     */
    private File copyFileFromJar(String jarPath, String filePreffix, String fileSuffix) throws IOException {
        File toFile = File.createTempFile(filePreffix, fileSuffix);
        // delete file after application finishes
        toFile.deleteOnExit();

        if(!toFile.canWrite()) throw new IOException("File (" + toFile.getPath() + ") not exists or is not writable!");

        FileOutputStream fos = new FileOutputStream(toFile);
        InputStream is = this.getClass().getResourceAsStream(jarPath);

        if(is == null) throw new IOException("File on jar path could not be located or loaded!");

        int read = 0;
        byte bytes[] = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            fos.write(bytes, 0, read);
        }

        fos.flush();
        fos.close();

        return toFile;
    }

    public static void main(String[] args) {
        new SWTFlashPlayer();
    }
}

I used the EdnCateDance.swf flash file which is one of the examples in SWT Flash library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文