سؤال

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. i have stored some images through netbeans and able to access them using image/imanename.jpg but can't able to store images. 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). You cannot simply save a file into the 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. Once you make the jar file, you cannot simply write files into the jar. Try and save your file in a normal directory (not within the jar).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top