通过 REST Web 服务发送字节数组和接收字符串

Send byte array and receive String through REST web service

在我的 Spring Rest Web 服务中,我将一个文件(甚至很大)作为字节数组发送,但是当我收到信息时,该对象是一个字符串,所以当我将对象从对象转换为字节[ ] 我收到以下错误:

java.lang.ClassCastException: java.lang.String cannot be cast to [B

原文件经过

转换

Files.readAllBytes(Paths.get(path))

而这个byte[]是用对象类型的字段result填充在一个对象中。 当客户端检索此对象并获得 result class 并强制转换为 byte[] 时出现上述异常,这是客户端代码

Files.write(Paths.get("test.txt"),((byte[])response.getResult()));

如果我使用转换为字符串然后转换为字节,则文件的内容与原始文件不同。我不关心文件类型,文件内容,我只需要从服务器复制到客户端目录 我该怎么办?谢谢

服务器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);
        }       
    }

而 FileTransfer 是:

    public class FileTransfer {

        private byte[] content;
        private String name;
..get and set

客户端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;
}

我相信这里的内容类型是text/plain,因此文件的内容是纯文本。只需从响应中生成字节数组:

 Files.write(Paths.get("test.txt"),((String)response.getResult()).getBytes());

所以客户端应该是这样的

@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;
    }