Reputation: 5
Hello I want to get a value of fileContent from the response string
My controller
@RestController
@Component
@RequestMapping("/rest")
public class SigningControllerREst {
@PostMapping("/uploadFile50")
public ResponseEntity<?> uploadFile50(@RequestParam("file") MultipartFile file)
throws FileNotFoundException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, RubricaException {
...
UploadFileResponse up=new UploadFileResponse(fileName, fileDownloadUri,
file.getContentType(), file.getSize(),result);
return new ResponseEntity<UploadFileResponse>(up, HttpStatus.OK);
}
The Client
File inFile = new File("C:\\Users\\admin-pc\\Desktop\\CV.pdf");
FileInputStream fis = null;
try {
fis = new FileInputStream(inFile);
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
// server back-end URL
HttpPost httppost = new HttpPost("http://localhost:8094/rubi/rest/uploadFile");
MultipartEntity entity = new MultipartEntity();
// set the file input stream and file name as arguments
entity.addPart("file", new InputStreamBody(fis, inFile.getName()));
httppost.setEntity(entity);
// execute the request
HttpResponse response = httpclient.execute(httppost);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
System.out.println(responseString );
} catch (ClientProtocolException e) {
System.err.println("Unable to make connection");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Unable to read file");
e.printStackTrace();
} finally {
try {
if (fis != null) fis.close();
} catch (IOException e) {}
}
The output
{"fileName":"CV.pdf","fileDownloadUri":"http://localhost:8094/rubi/downloadFile/CV.pdf","fileType":"application/octet-stream","fileContent":"Content in bytes","size":363629}
What I want is the value of fileContent (which was too long) from the responseString, How can I get that value .
Thanks for any help.
Upvotes: 0
Views: 7886
Reputation: 5
Solved by just changing the return object the returned object from UploadFileResponse
to byte[]
Changed this line from
return new ResponseEntity<UploadFileResponse>(up, HttpStatus.OK);
to
return new ResponseEntity<byte[]>(result, HttpStatus.OK);
Upvotes: 0
Reputation: 927
you can get it like this:
Map<String, Object> responseMap = new com.fasterxml.jackson.databind.ObjectMapper().readValue(fileContent, Map.class);
String fileContent = responseMap.get("fileContent").toString();
Upvotes: 1