The byte[] that is returned by FileUtils.readFileToByteArray is only the file contents, nothing else.

You should create your own class that is Serializable that includes two fields: a byte[] for the file contents, and a java.io.File that has everything else you need. You then serialize/deserialize your class into byte[], which is transmitted.

Answer from Glenn Lane on Stack Overflow
🌐
Java2s
java2s.com › example › java-utility-method › byte-array-from › getbytes-string-filename-c3abe.html
Java Byte Array from getBytes(String fileName)
j a v a2 s . c o m*/ public static byte[] getBytes(File file) { byte[] outputStr = null; BufferedInputStream buffStr = null; String fileName = ""; try { fileName = file.getName(); FileInputStream fileStr = new FileInputStream(file); System.out.println("File Stream: " + fileStr.available()); ...
🌐
Java2s
java2s.com › example › java-utility-method › file-name-get › getfilenames-byte-zipfile-e519b.html
Java File Name Get getFileNames(byte[] zipFile)
* #L% */ import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { /** * Gets the file names in a zip file */ public static Set<String> getFileNames(byte[] zipFile) throws IOException { Set<String> fileNames = new HashSet<>(); ByteArrayInputStream inputStream = new ByteArrayInputStream(zipFile); ZipInputStream zis = new ZipInputStream(inputStream); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { fileNames.add(entry.getName()); } return fileNames; } }
🌐
RoseIndia
roseindia.net › java › java-get-example › get-byte-array.shtml
Get Byte Array from File
Here is the video tutorial of "How to read a file in byte array in Java?": Here is the code of GetByteArrayFromfile.java · Output will be displayed as: Download Source Code · Java Get Example · Java Get Host Name · Java Get Month from Date · Java Get IP Address ·
🌐
Simplesolution
simplesolution.dev › java-convert-file-to-byte-array
Java Convert File to Byte Array - Simple Solution
In Java we can use Files.readAllBytes() method to read all content of a file into byte[] array. String fileName = "D:\\SimpleSolution\\data.txt"; Path filePath = Paths.get(fileName); byte[] allBytes = Files.readAllBytes(filePath);
Find elsewhere
🌐
Linux Hint
linuxhint.com › java-read-file-byte-array
Linux Hint – Linux Hint
September 8, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Java Code Geeks
javacodegeeks.com › home › core java
7 Examples to Read File into a byte array in Java - Java Code Geeks
April 27, 2020 - The FileUtils class from org.apache.commons.io package provides a general file manipulation facility like writing to a file or reading from a file. This method is used to read the contents of a file into a byte array, and the good thing about this is that the file is always closed. This is the classic way of reading the file’s content into a byte array. Don’t forget to close the stream once done. Here is the code to read a file into a byte array using FileInputStream class in Java:
🌐
How to do in Java
howtodoinjava.com › home › i/o › read file to byte[] in java
Read File to Byte[] in Java
December 14, 2022 - Let’s learn about a few ways of reading data from files into a byte array in Java. The Files.readAllBytes() is the best method for using Java 7, 8 and above. It reads all bytes from a file and closes the file.
🌐
Francomusso
francomusso.com › code-snippets › get-file-name-and-file-contents-as-byte-array
Get file name and file contents as Byte Array – Franco Musso
January 29, 2023 - function getFileContentsAndMetadata(entityID) { // Get the name of the selected file var fileName = encodeURIComponent(document.getElementById('file-upload').files[0].name); // Get the content of the selected file var file = document.getEle...
🌐
Microsoft
social.msdn.microsoft.com › Forums › vstudio › en-US › 4e6cf6ce-a236-42c2-bfb3-2ea85e34b232 › how-to-get-original-file-name-from-a-byte-array-contains-file-from-mac-or-windows
How to get original file name from a byte array (contains file from Mac or Windows) | Microsoft Learn
Getting the original filename from inside the byte array (Byte []) or · Writing the file as is to the local physical disk and then reading the file name via Path.GetFileName(). However the method File.WriteAllBytes requires a FileName !
🌐
Baeldung
baeldung.com › home › java › java io › convert file to byte array in java
Convert File to Byte Array in Java | Baeldung
January 5, 2024 - JDK provides several convenient ... into an array of bytes. For example, we can use the java.io or java.nio packages to answer our central question. So, let’s take a close look at each option. Let’s start with the easiest solution using the FileInputStream class from the IO package. Typically, this class comes with methods to read the content of a file as bytes. For instance, let’s assume we have a file named sample.txt ...
🌐
Usgs
usgs.github.io › pdl › coverage › gov.usgs.util › FileUtils.java.html
FileUtils.java
*/ public static String getContentType(final File file) { return getContentType(file.getName()); } /** * Get a file mime type based on its file path extension. * * Uses URLConnection.getFileNameMap(). * * @param filename * file path. * @return String mime type. */ public static String getContentType(final String filename) { return MIMETYPES.getContentTypeFor(filename); } /** * Read file contents into a byte array.
🌐
Medium
medium.com › @voziv › posting-a-byte-array-instead-of-a-file-using-spring-s-resttemplate-56268b45140b
POSTing a byte array instead of a file using Spring’s RestTemplate | by Lee Robert | Medium
January 18, 2018 - MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); map.add("name", filename); map.add("filename", filename); // Here we ByteArrayResource contentsAsResource = new ByteArrayResource(fileContents) { @Override public ...