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

Java 使用rest服务将文件从服务器端发送到客户端,java,spring,rest,file,spring-mvc,Java,Spring,Rest,File,Spring Mvc,我想使用rest服务将文件从服务器端发送到客户端 我正在使用SpringMVC。我使用了这种服务方式: public ResponseEntity<InputStreamResource> postFile() throws Exception { DocumentDaoImpl dao = new DocumentDaoImpl(); Document docCmis = (Document) dao.getDocument("workspace://Spac

我想使用rest服务将文件从服务器端发送到客户端 我正在使用SpringMVC。我使用了这种服务方式:

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


}
然后使用RestTemplate类调用rest,我试图获取我的文件

Map<String, Object> selectedCourrier=restTemplate.getForObject(SERVER_URI + "/getCourrierDetails"+"?id="+id, HashMap.class);
服务器端: 客户端:
private ResponseEntity getDownload(){
URI end=URI.create(“http://vmvdi05059:8080/ePaymentsWeb/download");
返回rest.getForEntity(end,byte[].class);
}
公共静态void main(字符串[]args)引发异常{
byte[]byteArray=新的TestClient().getDownload().getBody();
FileOutputStream fos=新建
FileOutputStream(“C:\\WorkSpace\\testClient\\abc.txt”);
fos.write(byteArray);
fos.close();
System.out.println(“文件写入成功…”);
}

请为您的控制器类提供代码,并说明哪些实际不起作用。请检查此问题是否对您有帮助。你可以按照这个@Optio我编辑了我的answer@AliakseiStadnik我使用了那个教程,但仍然不起作用,我只是用程序在使用那个图图时给我的日志编辑了我的问题
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..");

 }