2011-09-02 119 views
0

我正在使用eclipse中的SWT Widget Library進行Java編程,並且正在設計一個可運行的Java應用程序。我的應用程序已關閉,我只是不知道如何從「ALL」計算機上的文件夾加載外部.swf文件。我可以從任何計算機加載圖像,因爲我使用getResourceAsStream代碼行。但是「import com.docuverse.swt.flash.FlashPlayer」「loadMovie(arg,arg)」只接受一個字符串。因此,我做了ClassName.class.getResource(「blah.swf」)。getPath();}。這給你一個字符串,我設置它,並在eclipse上運行它可以完美地找到包中的文件。當我導出它時,我製作的可運行Jar無法在.jar文件中找到「blah.swf」。SWT-App從可運行的.jar或外部文件夾加載.swf文件

因此,我的問題是,如何從.jar或外部文件夾加載我的.swf文件,以便客戶端可以沿着.jar可執行應用程序下載,以便它可以指向這些swf文件。

謝謝。

+0

如果SWT FlashPlayer無法從流中加載電影,則必須從您從jar中獲取的流中創建臨時文件並由播放器加載它。 – Sorceror

+0

那麼流輸出「file:// C://Desktop//application.jar!//blah.swf「,你可以看到它試圖在應用程序內搜索。我想出了一個真正的骯髒的解決方案。我替換了「application.jar!」我在桌面上創建的名爲「SWF」的文件夾的名稱。問題是,如果應用程序和SWF文件夾位於兩個不同的目錄中,它將無法找到SWF的路徑。但請詳細說明您的評論,我對此很困惑。 – Jman

+0

你可以發佈鏈接到'com.docuverse.swt.flash.FlashPlayer' jar文件嗎?我找不到該項目的任何現有網站。然後我會嘗試製作[SSCCE](http://sscce.org/)。 – Sorceror

回答

1

這是我在評論中談到了(創建的臨時文件,並從罐子保存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閃存文件,該文件是在SWT閃光的例子之一圖書館。