从GET请求获取文档

时间:2017-11-15 12:26:28

标签: http-get cmis

我需要你的帮助: 我有以下方法

   @Path("/download")
    public class FileDownloadService {

        @GET
        public Response downloadFile(@QueryParam("filenet_id") String filenet_id, @QueryParam("version") String version) {
    ...
    Document document = (Document) cmisObject;
    return Response.ok(document, MediaType.APPLICATION_OCTET_STREAM).build();
}

我希望通过HTTP GET获取文档,我尝试编写此代码,但我不知道如何获取它,"输出"不要同意它:

URIBuilder builder = new URIBuilder();              
builder.setScheme("http").setHost("localhost:8080").setPath("/filenetintegration/rest/download")
    .setParameter("filenet_id", filenet_id)
    .setParameter("version", version+".0");
    URI uri = builder.build();
    HttpGet httpget = new HttpGet(uri);
    System.out.println(httpget.getURI());
    HttpURLConnection urlConnection = (HttpURLConnection) new URL(uri.toString()).openConnection();
    urlConnection.connect();
    BufferedReader br = new BufferedReader(new InputStreamReader((urlConnection.getInputStream())));
    String output;
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }

编辑: 也许问题出在这一行,它不会把文件放在回复中:

return Response.ok(document, MediaType.APPLICATION_OCTET_STREAM).build();

1 个答案:

答案 0 :(得分:0)

在服务器端你需要这样的东西:

Document document = (Document) cmisObject;

ContentStream contentStream = document.getContentStream();
final InputStream stream = contentStream.getStream();

StreamingOutput output = (OutputStream out) -> {
  try {
    int b;
    byte[] buffer = new byte[64*1024];

     while ((b = stream.read(buffer)) > -1) {
       out.write(buffer, 0, b);
     }
  } finally {
    try {
      stream.close();
    } catch (IOException ioe) {}
  }
};

return Response.ok(output, contentStream.getMimeType()).build();