-1

Currently I am shifted my server to linux from windows and I am facing some problem in my java webservice. In that I am creating a file in windows D drive. now I want to create that on linux server too. but I dont know how to give path and how to create (since it don't have drives as windows). so need some help for this. I am posting my java code below.

private static void receiveImg(String pic_bitmap) {
    FileOutputStream fos;
    try {

        fos = new FileOutputStream("D:\\AllImages\\ProfilePic\\Test.png");
        byte byteArray[] = Base64.decodeBase64(pic_bitmap);
        fos.write(byteArray);
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Thanks for any help.

minfo
  • 109
  • 1
  • 12
  • 1
    Linux does not use Windows drive letters, where on the Linux system do you want to store your files. I repeat, can't be on "D" drive. – Elliott Frisch Dec 24 '18 at 11:21
  • @Elliott Frisch thanks for suggestion. Can you tell how we can use url or path with port number in file path? – minfo Dec 24 '18 at 12:11
  • Possible duplicate of [How to read file on Windows and Linux from Java](https://stackoverflow.com/q/25307720/608639), [File path names for Windows and Linux](https://stackoverflow.com/q/20979625/608639), etc. – jww Dec 25 '18 at 00:12

2 Answers2

1

This should work. See below for more considerations

private static void receiveImg(String pic_bitmap) {
    FileOutputStream fos;
    try {

        fos = new FileOutputStream("/tmp/test.png");
        byte byteArray[] = Base64.decodeBase64(pic_bitmap);
        fos.write(byteArray);
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Note that you need to know the structure of the linux server to save it in a more suited place. If you can ssh onto the server you can run 'ls /' command to see the root folder structure. If you are not deploying the code yourself you may need a DevOps person to help you understand the folder structure on the destination server where you are deploying the code.

David
  • 755
  • 5
  • 11
  • thanks for the help. Can we use url or path with port number in file path? – minfo Dec 24 '18 at 11:52
  • Not with this code. Note: your code is a bit out of date. https://dzone.com/articles/fileinputstream-fileoutputstream-considered-harmful. Use Files.newOutputStream instead. With regards to a remote server, you'll need something like this: https://stackoverflow.com/questions/40590372/how-to-write-a-string-to-a-file-located-in-remote-server-linux – David Dec 24 '18 at 12:05
0

this could help you. It does not save an image, but that should not matter.

public void saveImage(String filename) { // filename ( path: linux ): /directory/filename 
    if(filename!=null){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
            oos.writeObject('objectToWrite');
            oos.flush();
            oos.close();
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }           
    }else{
        System.out.println("NullPointerException");
    }
}
Bernd
  • 593
  • 2
  • 8
  • 31