我有一个 apache cxf 休息服务,其中 on 方法需要接收图像和其他一些字符串数据。为此,我创建了一个这样的服务定义。
@Override
@POST
@Path("attach/{formId}/{instanceId}")
@Consumes("multipart/mixed")
@Produces("application/xml")
public UploadResult attach(@FormParam("username") @WebParam(name = "username") String user,
@FormParam("password") @WebParam(name = "password") String password,
@PathParam("formId") @WebParam(name = "formId") String formId,
@PathParam("instanceId") @WebParam(name = "instanceId") String instanceId,
@FormParam("group") @WebParam(name = "group") String group,
@FormParam("data") @WebParam(name = "data") byte[] data,
@FormParam("metadata") @WebParam(name = "metadata") byte[] metadata)
throws PDProcessingException,
PDSecurityException
{
return svc.attach( user, password, formId, instanceId, group, data, metadata );
}
现在我尝试对我的客户使用此方法,但它不起作用并返回 415 不受支持的媒体类型。我用 Wiztools.org RestClient 和一个测试客户端代码尝试了它,它调用了这样的方法
public static String attach(String username, String hashedpassword, String attachmentUri) {
String retVal = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.101.27/pdxapi/service/rest/ClientApp/attach/DE_OJ_PDIX_TEST/SRSQS.0");
try {
String hashedPw = DigestFactory.calculatePasswordHash(hashedpassword);
File file = new File(attachmentUri);
if (file == null || !file.exists()){
throw new RuntimeException("there is no file " + attachmentUri);
}
MultipartEntity entity = new MultipartEntity();
entity.addPart("username", new StringBody(username));
entity.addPart("password", new StringBody(hashedPw));
FileBody fileBody = new FileBody(file,"image/jpeg");
entity.addPart("data",fileBody);
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getEntity() == null){
retVal = "";
}
else{
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
sb.append(line);
}
retVal = sb.toString();
}
} catch (IOException e) {
System.out.print("exception " + e.getMessage());
}
return retVal;
}
我也尝试了注释 @Consumes("multipart/form-data") 但结果相同。有什么建议吗?
谢谢