動的データを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
...
}
}
.
アプリケーションのエンジンを変更することなく、必要に応じて画像の保存と読み取りを整理できます。