[英]send a file from the server side to the client side using rest service
我想使用Rest服務將文件從服務器端發送到客戶端,而我使用的是Spring MVC。 我使用了這種服務方法:
public ResponseEntity<InputStreamResource> postFile() throws Exception {
DocumentDaoImpl dao = new DocumentDaoImpl();
Document docCmis = (Document) dao.getDocument("workspace://SpacesStore/ae6d1722-0f08-49ab-a73b-c07036001318");
byte[] myByteArray = readContent(docCmis.getContentStream().getStream());
ClassPathResource myFile = new ClassPathResource(docCmis.getContentStreamFileName());
//System.out.println("eeeee"+pdfFile);
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity
.ok()
.headers(headers)
.contentLength(myByteArray.length)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(docCmis.getContentStream().getStream()));
}
和這個功能在rest控制器類中
@RequestMapping(value = "/downloadPDFFile",produces = { "application/json" }, method = RequestMethod.GET)
@ResponseBody
public ResponseEntity downloadPDFFile() throws Exception {
return courriersArrivésServices.postFile();
}
然后使用RestTemplate類進行一次休息電話,我試圖獲得我的文件
Map<String, Object> selectedCourrier=restTemplate.getForObject(SERVER_URI + "/getCourrierDetails"+"?id="+id, HashMap.class);
但這對我不起作用,並給我這個錯誤
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/octet-stream]
@RequestMapping("/download")
public byte[] download() throws Exception {
File f = new File("C:\\WorkSpace\\Text\\myDoc.txt");
byte[] byteArray = new byte[(int) f.length()];
byteArray = FileUtils.readFileToByteArray(f);
return byteArray;
}
private ResponseEntity<byte[]> getDownload(){
URI end = URI.create("http://vmvdi05059:8080/ePaymentsWeb/download");
return rest.getForEntity(end,byte[].class);
}
public static void main(String[] args) throws Exception {
byte[] byteArray = new TestClient().getDownload().getBody();
FileOutputStream fos = new
FileOutputStream("C:\\WorkSpace\\testClient\\abc.txt");
fos.write(byteArray);
fos.close();
System.out.println("file written successfully..");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.