如何在jar中动态添加图像?甚至可能吗?
在我的摇摆项目中,我让用户选择他的user_image
using JFileChooser
. 现在我无法使用数据库。所以我想如果我可以将上传的图像添加到 jar 中然后获取它。它应该是一种有效的方式吗?或任何其他想法来做到这一点?如何有效地保存图像,以便我的摇摆应用程序可以访问它。图片数量不固定,因为同一台机器上的多个用户将使用相同的 jar 文件上传图片。
如何在jar中动态添加图像?甚至可能吗?
在我的摇摆项目中,我让用户选择他的user_image
using JFileChooser
. 现在我无法使用数据库。所以我想如果我可以将上传的图像添加到 jar 中然后获取它。它应该是一种有效的方式吗?或任何其他想法来做到这一点?如何有效地保存图像,以便我的摇摆应用程序可以访问它。图片数量不固定,因为同一台机器上的多个用户将使用相同的 jar 文件上传图片。
我不确定将动态数据存储在 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
...
}
}
.
您可以根据需要组织图像的存储和读取,而无需更改应用程序的引擎。
我建议您有一个应用程序目录,您的应用程序从中读取图像并使用Preference-API使该目录可配置