im supposed to make a system using spring rest services inside containers, where you can upload mp4 videos to a service that stores them, and then preview them . how exactly would i send videos through a REST service in a post or maybe a put request ?
CodePudding user response:
You could try to make your file to a base64 encoded string that can be decoded by your RESTful api.
File to base64:
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(myFile);
objectOutputStream.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (IOException exception) {
throw exception;
}
Base64 to file:
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(base64));
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
File result = (File) objectInputStream.readObject();
objectInputStream.close();
return result;
} catch (Exception exception) {
throw exception;
}
In this example im using the Base64Coder
from snakeyaml
Gradle:
// https://mvnrepository.com/artifact/org.yaml/snakeyaml
implementation 'org.yaml:snakeyaml:1.30'