Java RESTEasy WebService - 从HTTPResponse对象下载映像文件。

时间:2016-07-19 01:26:57

标签: web-services

我需要使用RESTEasy Web服务从文件系统下载图像文件,输入httpclient是JSON,输出响应是   @Produces({"image/jpeg,image/png"})

这是我的客户代码:

public void getFileDownload(){  
        log("inside getServerPath....");
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(downloadWebService_URL);
        JSONObject json = new JSONObject();
        json.put("filePath", "/ngs/app/sample.png");
        json.put("fileName", "sample.png");

        log("json-->"+json.toString());

        StringEntity inputJson = null;
        try {
            inputJson = new StringEntity(json.toString());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        log("inputJson = " + inputJson.toString());

        inputJson.setContentType("application/json");
        httpPost.setEntity(inputJson);
        httpPost.addHeader("AppType", "TC");

        log("httpPost... httpPost");
        HttpResponse response = null; 
        try {
            response = httpClient.execute(httpPost);
            log("response:-->"+response);
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
        }
        catch (IOException e) { 
            e.printStackTrace();
        }

        catch (Exception e) 
        {
            log("E:: " + ExceptionUtils.getStackTrace(e));
        }
    }

这是我的Webservice代码:

@Path("/downloadservice")
public class DownloadFileWS {

  private static final String FILE_PATH = "/ngs/app/sample.png";

    @POST  
   // @Path("/images") 
    @Path("/{fileName}/images")
    @Consumes({"application/json"}) 
    @Produces({"image/jpeg,image/png"})
    public Response getImageFile(@PathParam("fileName") String fileName) {  
        File file = new File(FILE_PATH);    
        System.out.println("File requested is : " + fileName);
        Logger.getLogger("!!!!!!!!!!!"+FILE_PATH);
        System.out.println("@@@@@@@@"+FILE_PATH);
        ResponseBuilder response = Response.ok((Object) file);  
        response.header("Content-Disposition","attachment; filename=\"sample.png\""); 
        return response.build();  
    }  

HTTP响应是:

  

回复: - > HTTP / 1.1 200 OK [日期:星期二,2016年7月19日00:36:22 GMT,   Content-Length:6192,Content-Type:image / png,Content-Disposition:   附件; filename =“sample.png”,X-Powered-By:Servlet / 2.5 JSP / 2.1]   org.apache.http.conn.BasicManagedEntity@2ace1307

问题: 1.根据响应,看起来服务正在HTTPResponse对象中发送图像。我可以知道如何下载从HTTP响应中收到的图像吗? 2.要求是通过传递JSON作为输入请求来单击调用webservice的链接,图像应自动下载到用户的本地机器浏览器。

0 个答案:

没有答案