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:

 Python Basics How to check the version of Python interpreter mac terminal

Popular in last 30 days