![](/img/trans.png)
[英]How To Pass Multipart Image File as parameter to a POST REST Service
[英]Rest service does not accept the given post althoug it is annotated as multipart
我有一個apache cxf rest服務,其中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”),但結果相同。 有什么建議嗎?
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.