4

jarに画像を動的に追加する方法は? それは可能ですか?

私のスイングプロジェクトでは、ユーザーが使用して選択できるようにuser_imageしましたJFileChooser。現在、データベースを使用できません。アップロードした画像をjarに追加してから取得できると思います。それは有効な方法であるべきですか?またはこれを行うための他のアイデアはありますか?スイングアプリがアクセスできるように、画像を効率的に保存するにはどうすればよいですか。画像の数は固定されていません。これは、複数のユーザーが同じ jar ファイルを使用して同じマシンに画像をアップロードするためです。

4

2 に答える 2

0

動的データをjarに保存するのが良い考えかどうかはわかりません。動的ローカル アプリケーションを作成する場合は、いくつかの作業ディレクトリを使用できます。または、http 接続を使用してリモート イメージ ストレージを操作することもできます。または、 DBを使用できます。

または、次のような柔軟なインターフェイスのようなものを開発します。

public interface ImageProvider {
  public void storeImage(MyImageClass image, String imageLable);
  public MyImageClass getImage(String imageLable);
}

.

アプリケーションが開発されている限り、クラスごとに実装します。

public class ImageJarProvider implements ImageProvider { // jar solution
  private File jar = null;
  public ImageJarProvider(File jar) {
    this.jar = jar;
  }
  public void storeImage(MyImageClass image, String imageLable) {
     // implement jar repack:
     // use classes JarFile, ZipEntry and ZipOutputStream.
     // unpack file
     JarFile jarFile = new JarFile(jar);
     ZipEntry inputEntry = jarFile.getEntry("path/you/need/to/file");
     File outFile = new File("temp.jar");
     FileOutputStream zipFileStream = new FileOutputStream(outfile);
     ZipOutputStream zipOutStream = new ZipOutputStream(new BufferedOutputStream(zipFileStream));
     ZipEntry entry = new ZipEntry("filename you pack");
     zipOutStream.putNextEntry(entry);
     //pack all your files and pack new image.
     //this code just shows how to unpack and pack zip(jar)-archives.
     BufferedInputStream origin = new BufferedInputStream(new jarFile.getInputStream(inputEntry));
     byte data[] = new byte[2048];
     int count = 0;
     while((count = origin.read(data, 0, data.length)) != -1) {
       zipOutStream.write(data, 0, count);
     }
     zipOutStream.close();
     origin.close();
  }

  public MyImageClass getImage(String imageLable) {
     // implement unpack of image
     ...
  }
}

public class ImageHttpProvider implements ImageProvider { // http solution
  public ImageHttpProvider(String host, int port) {
     ...
  }

  public void storeImage(MyImageClass image, String imageLable) {
     // implement image upload
     ...
  }

  public MyImageClass getImage(String imageLable) {
     // implement HTTP image download
     ...
  }
}

public class ImageDirProvider implements ImageProvider { // working directory solution
  public ImageDirProvider(File dir) {
     ...
  }

  public void storeImage(MyImageClass image, String imageLable) {
     // implement file work
     ...
  }

  public MyImageClass getImage(String imageLable) {
     // implement file work
     ...
  }
}

public class ImageDBProvider implements ImageProvider { // DB solution
  public ImageDBProvider(String jdbcURL, Properties jdbcProperties) {
     ...
  }

  public void storeImage(MyImageClass image, String imageLable) {
     // implement jdbc clob
     ...
  }

  public MyImageClass getImage(String imageLable) {
     // implement jdbc clob
     ...
  }
}

.

アプリケーションのエンジンを変更することなく、必要に応じて画像の保存と読み取りを整理できます。

于 2011-03-01T09:30:53.167 に答える
0

アプリが画像を読み取るアプリ ディレクトリを用意し、 Preference-APIを使用してこのディレクトリを構成可能にすることをお勧めします。

于 2011-03-01T09:05:17.757 に答える