How to save an PNG via code in a Runnable JAR file?

77 views Asked by At

I'm having trouble fixing this issue, I have created a Client\Server side application and created a Method where a user can "Send" a PNG file from his side to Server side, then the Server side "Creates" and saves the image in a Package that only contains pictures.

When i run this Method of sending a Picture from Client side to Server side via Eclipse IDE it works as expected, but when exporting Client/Server side into Runnable JAR files, i get the next error:

Java

private static void getImg(MyFile msg) {
    int fileSize =msg.getSize();
      System.out.println("length "+ fileSize);
        try {
            File newFile = new File(System.getProperty("user.dir")+"\\src\\GuiServerScreens\\"+msg.getFileName());
            FileOutputStream fileOut;
            fileOut = new FileOutputStream(newFile);
            BufferedOutputStream bufferOut = new BufferedOutputStream(fileOut);
            try {
                bufferOut.write(msg.getMybytearray(), 0,  msg.getSize());
                fileOut.flush();
                bufferOut.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
}

I get the follow error :

java.io.FileNotFoundException: java.io.FileNotFoundException: C:\Users\Ilya\Desktop\src\GuiServerScreens\test.png (The system cannot

find the path specified)

It seems that using File newFile = new File(System.getProperty("user.dir")+"\\src\\GuiServerScreens\\"+msg.getFileName());

Does not provide the wanted result

1

There are 1 answers

1
Just another Java programmer On

I think you are mixing what your directories look like in netbeans with what's available on the server. Save to an external directory instead, not to your src directory.