[英]Jboss RestEasy - How do i extract binary data from a multipart mime MultipartInput using multipart/mixed
我有一個包含兩部分的MultipartInput
這是我如何從零件中提取數據的示例
@POST
@Path("/mixedMime")
@Consumes("multipart/mixed")
@ResponseStatus(HttpStatus.OK)
public String mixedMime(@Context ServletContext servletContext, MultipartInput input) throws Exception{
int index = 0;
String xmlText;
byte[] imageData;
for (InputPart part : input.getParts()) {
index++;
if(index==1){
//extract the xml test
xmlText = part.getBodyAsString()
}
if(index==2){
//extract the image data
imageData = part.getBody(???<<<<< WHAT GOES HERE >>>>???);
}
}
}
如何提取上面顯示的圖像數據(二進制數據)? 我正在使用Jboss 7.0.2。 根據http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Multipart.html上的文檔,是說我需要指定一個類? 什么級別?
謝謝
抱歉,我忘了包括我如何將數據發送到REST服務。 這是來自客戶端的相關代碼。 基本上,我從文件系統添加xml文件作為第一部分。 圖像作為第二部分。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/RestTest/rest/mixedmime");
Scanner scanner =
new Scanner(new File("myXmlFile.xml")).useDelimiter("\\Z");
String messageHeader = scanner.next();
scanner.close();
FileBody bin = new FileBody(new File("dexter.jpg"));
StringBody header = new StringBody(messageHeader.toString());
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("header", header);
reqEntity.addPart("payload", bin);
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
這是一個簡單的示例,說明如何使用RESTEasy處理多部分請求中的二進制(流)數據:
首先,也定義一個類來映射您的多部分表單:
public class DataUploadForm implements Serializable {
static final long serialVersionUID = IL;
@FormParam("xml")
private String xml;
@FormParam("file")
private InputStream fileStream;
public FileUploadForm() {
super();
}
// Getters and setters here
}
然后在您的Web服務接口上,聲明一個處理多部分內容並將其映射到您的自定義類的方法:
@POST
@Path("/somepath")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response uploadData(@MultipartForm DataUploadForm uploadForm);
在您的Web服務實現中,處理傳入的請求:
@Override
public Response uploadData(DataUploadForm uploadForm) {
System.out.printf("Incoming xml data: %s\n", uploadForm.getXML());
System.out.printf("Incoming binary data: %s\n", uploadForm.getFileStream());
// Processing the input stream. For example, by using Apache Commons IO
final byte[] data ;
try {
data = IOUtils.toByteArray(uploadForm.getFileStream());
} catch (IOException ioe) {
throw new WebApplicationException("Could not read uploaded binary data");
}
return Response.ok().build();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.