2

Glassfish で実行されている RESTful Web サービスにバイナリ ファイル (画像) を送信しようとしています。REST Web サービスの Upload data メソッド と他のいくつかの同様の投稿でそれを行うはずのコードを見つけまし たが、どちらも機能しません。これが私のコードです:

@POST
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String post( InputStream payload ) throws IOException
{
    return "Payload size="+payload.available();
}

@POST
@Path("bytes")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String post( byte[] payload )
{
    return "Payload size="+payload.length;
}

InputStream を受け取るメソッドは次を返します。

Payload size=0

byte[] を受け取るメソッドは次を返します。

Error 500 - Internal Server Error

エラー 500 は、次の例外によって発生します。

Caused by: java.lang.NullPointerException
at com.sun.jersey.moxy.MoxyMessageBodyWorker.typeIsKnown(MoxyMessageBodyWorker.java:110)
at com.sun.jersey.moxy.MoxyMessageBodyWorker.isReadable(MoxyMessageBodyWorker.java:133)
at com.sun.jersey.core.spi.factory.MessageBodyFactory._getMessageBodyReader(MessageBodyFactory.java:345)
at com.sun.jersey.core.spi.factory.MessageBodyFactory._getMessageBodyReader(MessageBodyFactory.java:315)
at com.sun.jersey.core.spi.factory.MessageBodyFactory.getMessageBodyReader(MessageBodyFactory.java:294)
at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:449)
at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46)
... 40 more

アドバイスをいただければ幸いです。

4

1 に答える 1

2

APPLICATION_OCTET_STREAM は機能していると思いますが、payload.available() はここでは機能しません

@POST
@Path("upload")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String uploadStream( InputStream payload ) throws IOException
{
    while(true) {
        try {
             DataInputStream dis = new DataInputStream(payload);
            System.out.println(dis.readByte());
        } catch (Exception e) {
            break;
        }
    }
    //Or you can save the inputsream to a file directly, use the code, but must remove the while() above.
  /**
    OutputStream os =new FileOutputStream("C:\recieved.jpg");
    IOUtils.copy(payload,os);
  **/
    System.out.println("Payload size="+payload.available());
    return "Payload size="+payload.available();
}

いくつかのバイトを出力するため、メソッドが実際に機能することがわかります。しかし、payload.available() は 0 です。

于 2013-05-27T02:10:40.397 に答える