in my Spring Rest web service I send a file (even big size) as byte array but when I receive the information, the object is a String so when I make the cast from Object to byte[] I receive the following error:
java.lang.ClassCastException: java.lang.String cannot be cast to [B
The originl file is converted through
Files.readAllBytes(Paths.get(path))
and this byte[] is filled in one object with a field result of Object type.
When the Client retrieve this object and it gets result class with cast to byte[] it appears the above exception, this is the client code
Files.write(Paths.get("test.txt"),((byte[])response.getResult()));
If I use a cast to string and then to bytes the content of the file is different from original file. I don't care the file type, file content, I only have to copy from server to client directory How can I do?Thanks
server class:
@Override
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody Response getAcquisition(@RequestParam(value="path", defaultValue="/home") String path){
try {
byte[] file = matlabClientServices.getFile(path);
if (file!=null){
FileTransfer fileTransfer= new FileTransfer(file, Paths.get(path).getFileName().toString());
return new Response(true, true, fileTransfer, null);
}
else
return new Response(false, false, "File doesn't exist!", null);
} catch (Exception e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in MatlabClientControllerImpl::getAcquisition :" + errorResponse.getStacktrace());
return new Response(false, false, "Error during file retrieving!", errorResponse);
}
}
and FileTransfer is:
public class FileTransfer {
private byte[] content;
private String name;
..get and set
client class:
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody Response getFile(@RequestParam(value="path", defaultValue="/home") String path){
RestTemplate restTemplate = new RestTemplate();
Response response = restTemplate.getForObject("http://localhost:8086/ATS/client/file/?path={path}", Response.class, path);
if (response.isStatus() && response.isSuccess()){
try {
@SuppressWarnings("unchecked")
LinkedHashMap<String,String> result= (LinkedHashMap<String,String>)response.getResult();
//byte[] parseBase64Binary = DatatypeConverter.parseBase64Binary((String)fileTransfer.getContent());
Files.write(Paths.get(result.get("name")), DatatypeConverter.parseBase64Binary(result.get("content")));
return new Response(true, true, "Your file has been written!", null);
} catch (IOException e) {
return new Response(true, true, "Error writing your file!!", null);
}
}
return response;
}
byte[]would be sent as Base64 encoded string. Given the right mapper you can get this asbyte[].Responseclass look like? It should not containObjectit should be abyte[]field."Q2lhbyBNb25kbw=="is otherwise just a string for the deserializer. If you tell it to deserialize e.g. aBinaryResponseclass that has abyte[]field it would do the datatype conversion stuff for you.Response<GenericType>(not sure if that works thouhg) or sub classes