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

时间:2011-09-02 14:07:32

标签: java jar flash swt

我在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文件。

三江源。

1 个答案:

答案 0 :(得分:1)

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

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库中的一个示例。