version: 2.7.4

@GetMapping("/")

public String index() throws Exception {

    ClassPathResource classPathResource = new ClassPathResource("key/testfile");

    InputStream inputStream = classPathResource.getInputStream();

    byte[] encodedPrivateKey = new byte[(int) inputStream.available()];

    inputStream.read(encodedPrivateKey);

    inputStream.close();

    return Arrays.toString(encodedPrivateKey);
}

The localhost:8080/ interface only does the simplest thing, outputting the byte array of the file.

When I test in the local development environment, the output is correct. [126, 13, 111, -115, -58, 93, -15, -97, 99, -66, 96, 111, 7, -26, -45, -125, 61, 95, -102, 108, -100, -116, -27, 122, 13, -90, 119, -109, -107, -116, -94, -23]

However, when I execute java -jar, the last byte of the read file is incorrect and is set to 0. [126, 13, 111, -115, -58, 93, -15, -97, 99, -66, 96, 111, 7, -26, -45, -125, 61, 95, -102, 108, -100, -116, -27, 122, 13, -90, 119, -109, -107, -116, -94, 0]

The test code is here , one of the simplest spring boot services.

Comment From: wilkinsona

There's a bug in your code that's reading the stream. Please refer to the javadoc of the available method:

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

The read(encodedPrivateKey) call read fewer bytes than were estimated to be available. You need to check the value returned by read and keep reading until it returns -1.