Java再次从字节数组转换为字符串到字节数组

时间:2017-09-28 13:41:05

标签: java arrays spring pdf byte

我有一个字节数组,我需要将其转换为String,以便将它在POJO中发送到另一个微服务。这个微服务应该将它再次转换为字节数组,最后转换为pdf文件。

我的代码:

创建字节数组并转换为字符串

const someObj = {
  test: function(){
     console.log('foo!');
  }
}

function test(){
   console.log('bar!');
}

someObj.test();
test();

微服务检索字符串并转换为PDF文件:

byte[] bytes = proIntegrationService.loadDownloadDocument(accountId, dcId);
        PDFDocument pdfDocument = new PDFDocument(dcId+".pdf", new String(bytes, "UTF-8"));
        return pdfDocument;

PDF输出如下所示:

//一些行

 @RequestMapping(method = RequestMethod.GET, value = "/offers/{offerId}/contract")
 @ApiOperation(value = "Download an offer contract")
 @ApiResponses(value = @ApiResponse(code = 200, message = ""))

 public ResponseEntity<byte[]> downloadOfferContract(
        @PathVariable Long offerId, HttpServletRequest request) throws IOException {

        JsonNode pdf = brokerageApplicationService.downloadOfferDocument(offerId, request);
        byte [] data =pdf.get("file").textValue().getBytes("UTF-8");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/pdf"));
        String filename = pdf.get("fileName").asText();
        headers.set("Content-disposition","attachment; filename=\""+filename+"\"" );
        headers.set("Content-Encoding", "UTF-8");
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        headers.setContentLength(data.length);
        ResponseEntity<byte[]> response = new ResponseEntity<>(data, headers, HttpStatus.OK);

        return response;


    }

结果是 - &gt;内容是“那里” - 所以PDF文件的大小约为7MB - 但一切都只是白色。

我在这里做错了什么?

0 个答案:

没有答案