处理来自弹簧支架控制器的多部分响应

时间:2020-08-11 13:42:10

标签: spring-boot rest spring-mvc

我有这样的控制器方法

    @PostMapping(path = "/downloadAttachment",
        produces = "application/octet-stream")
    public ResponseEntity<?> downloadAttachment(@Valid @RequestBody Attachment attachmentModel) {
    refreshProp(false);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    try {
        String byteRes = null;
        JSONArray responseFromDownloadAttachment = 
        databaseOperations.downloadAttachment(attachmentModel);
        if (responseFromDownloadAttachment.length() == 0) {
            return new ResponseEntity<>("", HttpStatus.NO_CONTENT);
        }

        else {
            for (int blobRes = 0; blobRes < responseFromDownloadAttachment.length(); blobRes++) {
                JSONObject blobObj = responseFromDownloadAttachment.getJSONObject(blobRes);
                if (blobObj != null) {
                    byteRes = (String) blobObj.getString("file");
                }

            }
        }
        byte[] byteArrray = byteRes.getBytes();
        return new ResponseEntity<>(byteArrray, HttpStatus.OK);
    } catch (Exception e) {
        log.error("Exception occurred!" + e);
        e.printStackTrace();
        JSONObject errObj = new JSONObject();
        errObj.put("status", "E");
        errObj.put("message", e);
        return new ResponseEntity<>(errObj.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

我正在发送字节数组作为响应。但是我不确定我将从服务层获取哪种类型的文件。它可以是xlsx,txt,png,jpg或任何多媒体的任何形式。我正在设置标头八位字节流,也产生八位字节流。我可以使用八位字节流来处理这些类型的响应吗?

0 个答案:

没有答案