2015-11-24 66 views
0

在我的Spring Rest Web服务中我发送一个文件(甚至是大尺寸)作为字节数组,但是当我收到信息时,该对象是一个String,所以当我从目的是字节[]我收到以下错误:发送字节数组并通过REST Web服务接收字符串

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

的originl文件通过

Files.readAllBytes(Paths.get(path))

转换和这byte[]填充在一个对象与对象类型的字段result 。 当客户端取回该对象和它得到result类采用铸造到它出现在上面的异常byte[],这是客户机代码

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

如果我使用强制转换将字符串转换为字节,则文件内容与原始文件不同。我不在乎的文件类型,文件的内容,我只有从服务器复制到客户端目录 我该怎么办谢谢

服务器类:

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

与文件传输是:

public class FileTransfer { 

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

客户端类:

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

你能告诉你如何定义其余端点等?它接受哪种内容类型?如果它是json,那么'byte []'将作为Base64编码的字符串发送。给定正确的映射器,你可以把它作为'byte []'。 – zapl

+0

是的,我正在使用json。我更新了上面的代码 – luca

+0

你的'Response'类是怎么样的?它不应该包含Object,它应该是一个byte []字段。 – zapl

回答

1

所以客户应该是这样的

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

它的工作原理很棒。有没有办法检索文件名和扩展名(否则我不知道原始文件)?或者我有tu使用名称和文件的对象? – luca

+0

你必须使用名称和文件的对象..字节数组只是文件的内容 – awsome

+0

我更新与新的和工作的代码,你认为这是正确的?我不得不使用LinkedHashMap 来代替FileTrasfer,否则我会收到异常 – luca

0

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

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

正如我在主帖中写道,如果我投射到字符串和使用getBytes我收到不是“Hello world”,但“Q2lhbyBNb25kbw ==”。 – luca

+0

@luca是“Ciao Mondo”的Base64 - 见awsome。当将字符串编码为字节时, –

+0

可能希望在指定字符集时显式指定。 – asgs

相关问题