简体   繁体   English

Jboss RestEasy-如何使用multipart / mixed从multipart mime MultipartInput提取二进制数据

[英]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

  • Part1 - XML string 第1部分-XML字符串
  • Part2 - Binary data (an image) 第2部分-二进制数据(图像)

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 谢谢

Edit 编辑

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.

相关问题 部署时使用RESTEasy Multipart时出错 - Error using RESTEasy Multipart when I deploy 如何使用RestEasy框架发送Multipart响应 - How to send Multipart response using RestEasy framework 如何使用 org.jboss.resteasy.client.ClientRequest 上传多部分文件? - How to upload a multipart file using org.jboss.resteasy.client.ClientRequest? GAE / J:如何将多部分MIME消息从appengine发布到facebook - GAE/J: How do I POST a Multipart MIME message from appengine to facebook 如何在球衣中使用嵌套的多部分/混合mime类型 - How to consume a nested multipart/mixed mime type in jersey 如何使用Apache HttpComponentst创建和发布多部分/混合http请求? - how do i create and post a multipart/mixed http request using Apache HttpComponentst? 如何从多部分/混合请求中获取所有多部分文件? - How to obtain all multipart files from multipart/mixed request? MIME类型为multipart / mixed的无对象DCH - No object DCH for MIME type multipart/mixed 如何使用 java 创建多部分/混合请求 - how to create multipart/mixed request using java 如何使用RESTeasy从multipart / form-data请求获取text / xml为UTF-8? - How to get text/xml as UTF-8 from a multipart/form-data request with RESTeasy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM