Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4110

Image byte array not working as part of rest API call

$
0
0

I am trying to get a byte array of an image as part of the rest API call. The rest api retrieves the object from GCP Bucket and sends the byte array back as a response object. The thing is before sending the byte array, I tried writing it back to a image file to make sure it is working fine and it worked fine, I was able to open the image created from the byte array.But after receiving the byte array from server, I am trying to do the same thing by writing back to a image file, this time it shows unsupported type when I click on the file. Not sure why its happening. I have provided by server and client code below.Can someone please throw some light on it.

Working Server Code

public byte[] readDocument(String filename, String key) throws IOException {    try {        log.info("going to read document");        Storage storage = StorageOptions.getDefaultInstance().getService();        Blob blob = storage.get(environmentConfig.getBucket(), key+"/"+filename);        if(blob !=null) {            byte[] content = blob.getContent();            //FileUtils.writeByteArrayToFile(new File("D://Image-References/waters"+ Math.random()+".jpeg"),content);            log.info("Going to return content");            return content;        }else {            log.info("Going to return null");            return null;        }    }catch (Exception e){        e.printStackTrace();        throw e;    }}

Spring Rest API Code which sends data to client

@GetMapping(path="issue-doc-read-controller")public ResponseEntity readDoc(@PathParam("issueId")Long issueId, @PathParam("documentName")String  documentName) throws IOException {        IssueProjection byIssueId = issueRepository.findByIssueId(issueId);            return ResponseEntity.ok( storage.readDocument(                    documentName, Long.                            toString(issueId))            );    }

NOT WORKING CLIENT CODE

                    byte[] issueDocData = issueProvideService.getIssueDocData(data.getIssueId(), issueDocMetaData.getDocumentName());**//// This line is not working**                    FileUtils.writeByteArrayToFile(new File("D://fiXit//Image-References/waterFromWeb"+ Math.random()+".jpeg"),issueDocData);

Viewing all articles
Browse latest Browse all 4110

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>