动态添加jar中的图片

发布于 2024-10-19 10:48:34 字数 251 浏览 2 评论 0原文

如何动态添加jar中的图片?有可能吗?

在我的 swing 项目中,我让用户使用 JFileChooser 选择他的 user_image。现在我无法使用数据库。所以我想是否可以将上传的图像添加到jar中然后获取它。这应该是一个有效的方法吗?或者还有其他想法可以做到这一点吗?如何有效地保存图像以便我的 swing 应用程序可以访问它。图像的数量不固定,因为图像将由同一台计算机上的多个用户使用相同的 jar 文件上传。

How to add images in jar dynamically? Is it even possible?

In my swing project I let the user select his user_image using JFileChooser. Now I'm not able to use database. So I think if I can add the uploaded image in jar and then fetch it. Should it be a valid way? Or any other idea to do this? How can I efficiently save images so that my swing app can access it. The number of images is not fixed cause images will be uploaded by multiple user on the same machine using same jar file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-10-26 10:48:34

我建议您有一个应用程序从中读取图像的应用程序目录,并使用 首选项 API

I would suggest you to have an app directory from which your app is reading images and make this dir configurable using Preference-API

烟花易冷人易散 2024-10-26 10:48:34

我不确定将动态数据存储在 jar 中是个好主意。如果您创建动态本地应用程序,您可以使用一些工作目录。或者您可以使用 http 连接来处理远程图像存储。或者您可以使用数据库

或者您开发一些灵活的界面,例如:

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

.

您可以根据需要组织图像的存储和读取,而无需更改应用程序的引擎。

I'm not sure store dynamic data in jar is good idea. If you create dynamic local application you can use some working directory. Or you can use http connection to work with remote images storage. Or you can use a DB.

Or you develop something like flexible interface such as:

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

.

And implement it by classes as long as your application is being developed:

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

.

You can organize storing and reading of images as you want without changes in engine of your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文