HttpURLConnection Post Request,使用来自javax.rs.ws.Response对象的响应进行响应

时间:2015-06-03 21:08:40

标签: java web-services http httpurlconnection bytearrayinputstream

我正在尝试将一个Web服务调用为第二个Web服务,第一个是瘦访问层。我的底层网络服务看起来像这样,

@POST
@Path("/savechart")
@Produces("image/png")
public Response saveChart(@FormParam("chartId") String chartId, @FormParam("chartType") String chartType,
                          @FormParam("chartSubType") String chartSubType, @FormParam("authJsonMap") String authJsonMap,
                          @FormParam("chartParameters") String parameters, @FormParam("chartLocation") String chartLocation) {

    try {
        // String dataDetails = " Saved Chart ";
        if (chartId != null && !"".equalsIgnoreCase(chartId)) {

        }

        // SubProcessService subProcess = DaoRegistry.getSubProcessService();
        PhantomHelper p = new PhantomHelper("phantomjs");

        BufferedImage chart = p.saveChart(chartLocation , chartId, chartType, chartSubType, authJsonMap, parameters);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();

        ImageIO.write(chart, "png", bao);

        byte[] imageData = bao.toByteArray();

        if (imageData == null) {
            return Response.status(210).build();
        }

        p.kill();
        // the request is received
        return Response.ok(new ByteArrayInputStream(imageData))
                .build();
    }
    catch (Exception exe)
    {
        System.out.println("Save Chart Exception: " + exe.toString());
        System.out.println("Save Chart Exception: " + exe.getStackTrace().toString());
        exe.printStackTrace(System.out);
        return Response.status(500).entity("Error Saving Chart : " + exe.getStackTrace() ).build();
    }

}

它是一个Web服务,它创建一个png文件并在一个响应对象中发回该图像,该对象具有ByteArrayInputStream()作为其实体。如果我直接向它发送POST请求,这是有效的,但我想通过一个单独的Web服务。这是我试图在回答中阅读,然后反刍。

    public Response POST() throws Exception {

    // Create Post Request
    URL requestUrl = new URL(url);
    connection = (HttpURLConnection) requestUrl.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent" , USER_AGENT);
    connection.setRequestProperty("Accept-Language" , ACCEPT_LANGUAGE);

    // User assigned properties to header
    if (requestProperties != null) {
        for (String key : requestProperties.keySet()) {
            connection.setRequestProperty(key , requestProperties.get(key));
        }
    }

    // Construct URL
    String urlParameters = constructUrl(parameters);

    connection.setDoOutput(true);

    DataOutputStream out = new DataOutputStream(connection.getOutputStream());

    out.writeBytes(urlParameters);
    out.flush();
    out.close();

    // BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    return Response.status(200).entity(connection.getInputStream()).build();

}

但这不起作用。我无法弄清楚如何使用响应,将其转换回响应对象并通过它发送。

1 个答案:

答案 0 :(得分:1)

我通过将响应作为字节数组读入然后使用类似的ByteArrayOutputStream进行响应来修复此过程。代码如下

public Response POST() throws Exception {

    // Create Post Request


    URL requestUrl = new URL(url);
    connection = (HttpURLConnection) requestUrl.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent" , USER_AGENT);
    connection.setRequestProperty("Accept-Language" , ACCEPT_LANGUAGE);

    // User assigned properties to header
    if (requestProperties != null) {
        for (String key : requestProperties.keySet()) {
            connection.setRequestProperty(key , requestProperties.get(key));
        }
    }

    // Construct URL
    String urlParameters = constructUrl(parameters);


    connection.setDoOutput(true);

    OutputStream outStream = connection.getOutputStream();

    DataOutputStream out = new DataOutputStream(outStream);

    out.writeBytes(urlParameters);
    out.flush();
    out.close();


    byte[] input = IOUtils.toByteArray(connection.getInputStream());

    return Response.status(200).entity(new ByteArrayInputStream(input)).build();

}