2012-01-18 3 views
6

CXFでREST Webサービスとしてファイルアップロードハンドラを作成する必要があります。私は、次のようなコードを使用してメタデータを1つのファイルをアップロードすることができました:CXFで複数のファイルとメタデータをアップロードする

@POST 
@Path("/uploadImages") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadImage(@Multipart("firstName") String firstName, 
     @Multipart("lastName") String lastName, 
     List<Attachment> attachments) { 

    for (Attachment att : attachments) { 
     if (att.getContentType().getType().equals("image")) { 
      InputStream is = att.getDataHandler().getInputStream(); 
      // read and store image file 
     } 
    } 

    return Response.ok().build(); 
} 

は、今私は、同じリクエストで複数のファイルをアップロードするためのサポートを追加する必要があります。この場合、コンテンツタイプがimage/jpegの添付ファイルの代わりに、multipart/mixedコンテンツタイプの添付ファイルがあります。添付ファイルには、必要な添付ファイルimage/jpegが含まれています。

メタデータを使用して複数のJSONまたはJAXBオブジェクトをアップロードする例がありますが、バイナリイメージデータを処理するためのものは取得できませんでした。私はMultipartBodyを直接使用しようとしましたが、添付ファイルimage/jpegではなく、multipart/mixedの添付ファイルしか返しません。

multipart/mixed添付ファイルを再帰的に解析して埋め込み添付ファイルを取得する方法はありますか?私はもちろん、multipart/mixed添付ファイルの入力ストリームを取得し、ファイルを自分自身で解析することができますが、より良い方法があることを期待しています。

UPDATE

これはkludgeyようだが、次のコードビットは、今のところ十分です。私はよりよい方法を見たいと思う。

for (Attachment att : attachments) { 
    LOG.debug("attachment content type: {}", att.getContentType().toString()); 

    if (att.getContentType().getType().equals("multipart")) { 
     String ct = att.getContentType().toString(); 
     Message msg = new MessageImpl(); 
     msg.put(Message.CONTENT_TYPE, ct); 
     msg.setContent(InputStream.class, att.getDataHandler().getInputStream()); 
     AttachmentDeserializer ad = new AttachmentDeserializer(msg, Arrays.asList(ct)); 
     ad.initializeAttachments(); 

     // store the first embedded attachment 
     storeFile(msg.getContent(InputStream.class)); 

     // store remaining embedded attachments 
     for (org.apache.cxf.message.Attachment child : msg.getAttachments()) { 
      storeFile(child.getDataHandler().getInputStream()); 
     } 
    } 
    else if (att.getContentType().getType().equals("image")) { 
     storeFile(att.getDataHandler().getInputStream()); 
    } 
} 
+0

を使用して簡単に変換することができ、入力の別れアレイを好むならば私の実装では、パラメータを定義しようとしています(多分それは役立ちます)、次の

@Consumes({MediaType.MULTIPART_FORM_DATA,"multipart/mixed" }) public Response uploadImages(final List<Attachment> attachments) { Map<String, InputStream> imageMap = new HashMap<String, InputStream>(); for (Attachment attachment : attachments) { String imageName = attachment.getContentDisposition().getParameter("filename"); if (imageName == null) { imageName = UUID.randomUUID().toString(); } InputStream image = attachment.getDataHandler().getInputStream(); imageMap.put(imageName, image); } return imageMap; } 

のように見えます最終的な@Multipart( "image")リストのようにするか、コンテンツタイプに問題がありますか? – AxelTheGerman

+0

@axel添付ファイルリストにMultipart注釈を追加すると、CXFはnull値を渡すだけです。私はイメージを取得するために装飾されていないままにする必要があります。 –

答えて

0

複数の画像をアップロードするために同様のサービスを構築しました。誰かが代わりにストリーム、それはこのヘルパーメソッド

private static byte[] extractByteArray(final InputStream inputStream) throws IOException { 
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 

    byte[] dataChunk = new byte[1024 * 16]; 
    int numRead = 0; 
    while (numRead != -1) { 
     numRead = inputStream.read(dataChunk, 0, dataChunk.length); 

     if (numRead != -1) { 
      buffer.write(dataChunk, 0, numRead); 
     } 
    } 

    buffer.flush(); 
    return buffer.toByteArray(); 
} 
関連する問題