简体   繁体   English

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

[英]send a file from the server side to the client side using rest service

I want to send a file from the server side to the client side using rest service I'm using spring MVC. 我想使用Rest服务将文件从服务器端发送到客户端,而我使用的是Spring MVC。 I used this service method: 我使用了这种服务方法:

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()));


}

and this function in a rest controller class 和这个功能在rest控制器类中

@RequestMapping(value = "/downloadPDFFile",produces = { "application/json" }, method = RequestMethod.GET)
@ResponseBody
public ResponseEntity downloadPDFFile() throws Exception {
    return courriersArrivésServices.postFile();

}

then with a rest call using RestTemplate class,i tryied to get my file 然后使用RestTemplate类进行一次休息电话,我试图获得我的文件

Map<String, Object> selectedCourrier=restTemplate.getForObject(SERVER_URI + "/getCourrierDetails"+"?id="+id, HashMap.class);

But that didn't work for me and gives me this error 但这对我不起作用,并给我这个错误

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]

Server side: 服务器端:

@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;
}

Client side: 客户端:

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.

相关问题 使用GWT将Excel文件从服务器端发送到客户端 - Send excel file from server-side to client-side using GWT 使用Dojo文件上传客户端将其他数据发送到Java服务器端 - Send additional data to Java server-side using Dojo file upload client-side REST中的客户端服务器通信:使用RestTemplate:无法将数据发送到服务器端 - Client Server Communication in REST: Using RestTemplate: Unable to send data to Server side 使用Spring和Rest Web服务将文件从服务器发送到Client Java - Send file from Server to Client java with Spring and Rest web service 使用ajax和spring portlet将文件从客户端上传到服务器端 - Upload a file from client side to server side with ajax and spring portlet 无法从客户端删除服务器中的文件? - not able to delete file in server from client side? 我如何使用jsp servlet从服务器端将Excel文件下载到客户端 - how can i download an excel file from server side to client side using jsp servlet 使用Java套接字将文件从服务器传输到客户端。 服务器端错误,文件传输到客户端为空 - File Transfer from Server to Client using java sockets. Error on Server side and File transferred to client is empty 如何从客户端发送用于Web服务身份验证的用户名密码 - How to send username password for web service authentication from client side 从客户端将文件作为参数发送到REST服务? - Send File as a parameter to a REST Service, from a client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM