我正在尝试让一个 Web 服务调用第二个 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();
}
但这不起作用。我不知道如何使用响应,将其转换回响应对象并通过它发送。