0

I want to create an image file of byte array, image file save in resources folder. Then, i want to show image file in my panel.

I wrote this code, this code works in Eclipse well but when i create a jar file and run jar with command in windows, i get error.

I searched, someone said, i should use getResourceAsStream but i don't know how can use this. Please help me. Thanks

Method for saving image:

public static void saveImage() {
   String s="........................";
   byte[] dataCustImg = Base64.decode(s.getBytes());

   URL url = st.getClass().getClassLoader().getResource("resources/");
   File file = new File(url.toURI());
   try {
    OutputStream stream = new FileOutputStream(file+"/test"
                + sData.getCustID() + ".bmp");

    stream.write(dataCustImg);
    stream.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

}

Error is for below line:

    File file = new File(url.toURI());

Error:

   Exception in thread "Thread-1" java.lang.IllegalArgumentException: URI is not hi
    erarchical
    at java.io.File.<init>(Unknown Source)
    at com.kit.vip.popup.ServerThread.saveImage(ServerThread.java:114)
    at com.kit.vip.popup.ServerThread.run(ServerThread.java:90)

I write byte array to image file inside jar file, this is true?

Thanks for your answers.

Fahim
  • 384
  • 5
  • 20
  • The `url` refers to a location inside your jar file. You cannot write a new file inside your jar (or zip) file like that using `FileOutputStream`. You can only read data using `getResourceAsStream`. – Codebender May 24 '16 at 09:32
  • Thanks for your reply. but how can save my image outside jar and retrieve to this? I want secure – Fahim May 24 '16 at 09:36
  • my bug for writing image inside jar? but my error above writing image! – Fahim May 24 '16 at 09:37

0 Answers0