[英]Send byte array and receive String through REST web service
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: 在我的Spring Rest Web服务中,我以字节数组的形式发送一个文件(甚至很大),但是当我收到信息时,对象是一个字符串,因此当我从Object转换为byte []时,会收到以下错误:
java.lang.ClassCastException: java.lang.String cannot be cast to [B java.lang.ClassCastException:无法将java.lang.String强制转换为[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. 并将此byte[]
填充到一个对象中,并使用对象类型的字段result
。 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 当客户端检索到该对象并使用强制转换为byte[]
result
类时,出现上述异常,这是客户端代码
Files.write(Paths.get("test.txt"),((byte[])response.getResult()))
; 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: FileTransfer是:
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;
}
So the client should be something like this 所以客户应该是这样的
@RequestMapping(value = "/test/", method = RequestMethod.GET)
public Response getFileTest(@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 {
byte[] parseBase64Binary = DatatypeConverter.parseBase64Binary((String)response.getResult());
Files.write(Paths.get("test.txt"),parseBase64Binary );
} catch (IOException e) {
}
}
return response;
}
I believe the content-type here is text/plain
, therefore the content of the file is a plain text. 我相信这里的内容类型是text/plain
,因此文件的内容是纯文本。 Simply generate byte array from the response: 只需从响应生成字节数组:
Files.write(Paths.get("test.txt"),((String)response.getResult()).getBytes());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.