Add images to the bank dynamically

How to add images to the jar dynamically? Is it possible?

In my swing project, I let the user select it user_imagewith JFileChooser. Now I can not use the database. Therefore, I think that if I can add the uploaded image to the jar, then get it. Should it be valid? Or any other idea to do this? How can I efficiently save images so that my swing application can access it. The number of images is not fixed because the images will be downloaded by several users on the same computer using the same jar file.

+3
source share
2 answers
0

, - . , . 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
     ...
  }
}

.

, , .

0

Source: https://habr.com/ru/post/1795431/


All Articles