[英]Jboss RestEasy - How do i extract binary data from a multipart mime MultipartInput using multipart/mixed
I have a MultipartInput that has two parts 我有一个包含两部分的MultipartInput
Here is an example of how i extract the data from the parts 这是我如何从零件中提取数据的示例
@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 >>>>???);
}
}
}
How would i extract the image data (binary data) shown above? 如何提取上面显示的图像数据(二进制数据)? I am using Jboss 7.0.2.
我正在使用Jboss 7.0.2。 According to the documentation at http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Multipart.html , it is saying i need to specify a class?
根据http://docs.jboss.org/resteasy/docs/2.3.0.GA/userguide/html/Multipart.html上的文档,是说我需要指定一个类? what class?
什么级别?
Thanks 谢谢
Sorry i forgot to include how i am sending the data to the REST service. 抱歉,我忘了包括我如何将数据发送到REST服务。 Here is the relevant code from the client.
这是来自客户端的相关代码。 Basically i add the xml file from the file system as the first part.
基本上,我从文件系统添加xml文件作为第一部分。 An an image as the second part.
图像作为第二部分。
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);
Here's a simple example of how you would process binary (stream) data out of a multipart request with RESTEasy: 这是一个简单的示例,说明如何使用RESTEasy处理多部分请求中的二进制(流)数据:
First, define a class to map your multipart form too: 首先,也定义一个类来映射您的多部分表单:
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
}
Then on your web service interface, declare a method that handles multipart content and maps it to your custom class: 然后在您的Web服务接口上,声明一个处理多部分内容并将其映射到您的自定义类的方法:
@POST
@Path("/somepath")
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response uploadData(@MultipartForm DataUploadForm uploadForm);
And in your web service implementation, process the incoming request: 在您的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.