Best way to send a file over a network between two applications in Java


On the sender application side Pass the path to the file as the argument to the following method. The output is byte array which is then serialized and send across the network.
    public static byte[] readFileAsBytes(String pathToFile) {

        byte[] data = null;
        try {
            Path path = Paths
                    .get(pathToFile);
            data = Files.readAllBytes(path);
        } catch (Exception e) {

        }

        return data;
    }
On the receiver application side Get the byte array and pass it on the method.
 public static void convertBytesToFile(String name, byte[] byteArray) {

        try {
            FileOutputStream fileOuputStream = new FileOutputStream(name);
            fileOuputStream.write(byteArray);
            fileOuputStream.close();
        } catch (Exception e) {
        }
    }
Hope this helps:)

No comments:

 My Attempt to learn AI  GPT Generative Pretrained Transformer Created by Google in 2017 with the publication of the paper "Attention ...

Popular in last 30 days