简体   繁体   English

在运行时将图像存储在netbeans中制成的jar文件中

[英]storing image in jar file made in netbeans at runtime

i am making a program in which i have to allow the user to browse an image and store it with its details in image folder inside the source cod my code is working working in netbeans but when i make jar file the code is cot able to store image. 我正在制作一个程序,其中我必须允许用户浏览图像并将其详细信息存储在源代码中的图像文件夹中,我的代码在netbeans中工作,但是当我创建jar文件时,该代码可以存储图片。 i have stored some images through netbeans and able to access them using image/imanename.jpg but can't able to store images. 我已经通过netbeans存储了一些图像,并且能够使用image / imanename.jpg访问它们,但无法存储图像。 help me as soon as possible. 尽快帮助我。 thank you the code i tried is 谢谢你我尝试的代码是

            File f = new File(s);
            long size=f.length();
            FileInputStream fis1=new FileInputStream(f);
            FileOutputStream fos2=new FileOutputStream("image/"+tfpn.getText()+".jpg");
            byte b[]=new byte[10000];
            int r=0;
            long count=0;
            while(true)
            {
                r=fis1.read(b,0,10000);
                fos2.write(b,0,10000);
                count = count+r;
                if(count==size)
                break;
                System.out.println(count);
            }
            System.out.println("File copy complete");

When you generate a jar file, it is in a compressed format (like .zip). 生成jar文件时,该文件采用压缩格式(例如.zip)。 You cannot simply save a file into the jar. 您不能简单地将文件保存到jar中。 It is technically possible, but is complicated. 从技术上讲这是可能的,但是很复杂。 See here for more info. 有关更多信息,请参见此处

The reason it works in netbeans is because when you run a file in netbeans, the code is not yet compressed into a jar. 它在netbeans中起作用的原因是,当您在netbeans中运行文件时,代码尚未压缩到jar中。 Once you make the jar file, you cannot simply write files into the jar. 一旦创建了jar文件,就不能简单地将文件写入jar。 Try and save your file in a normal directory (not within the jar). 尝试将文件保存在普通目录中(不在jar中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM