繁体   English   中英

使用rest服务将文件从服务器端发送到客户端

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM