Java 如何解码从服务器返回的mulitpart?

Java 如何解码从服务器返回的mulitpart?,java,apache,rest,multipart,Java,Apache,Rest,Multipart,现在,我的服务器端代码响应一个多部分对象,如下所示(使用ApacheWinkforREST服务): 我试着这样解码: @Test public void test() throws ClassNotFoundException { HttpClient client = new HttpClient(); GetMethod get = new GetMethod( "http://DongyangLuo-PC.cn.ibm.com:9080/Cl

现在,我的服务器端代码响应一个多部分对象,如下所示(使用ApacheWinkforREST服务):

我试着这样解码:

    @Test
public void test() throws ClassNotFoundException {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(
            "http://DongyangLuo-PC.cn.ibm.com:9080/Cloud_Modeler/datasource/multipart");
    try {
        int statusCode = client.executeMethod(get);
        if (statusCode == 200) {
//            I instantiate the parser and try to instantiate the
//                InMultiPart object with the parser
            MultiPartParser parser = new MultiPartParser(
                    get.getResponseBodyAsStream(), "myboundary");

            InMultiPart ins = new InMultiPart(parser);
            InPart part = ins.next();
            System.out.println("The content type is: "
                    + part.getContentType());
            InputStream input = part.getInputStream();
//              this kind of decoding will get java.io.StreamCorruptedException: invalid stream header: 48656C6C
            byte[] ba = (byte[]) new ObjectInputStream(input).readObject();
            for(int j = 0; j < ba.length; j++){
                System.out.print(ba[j] + "\t");
            }
            System.out.println("The content body is: "
                    + part.getBody(String.class, String.class));
            System.out.println("The content headers are: "
                    + part.getHeaders());
            part = ins.next();
//              this kind of decoding doesn't work because of losing a provider which shouldn't appear in client.
            byte[] body = part.getBody(byte[].class, byte[].class);
            System.out.println(part.getHeaders());
            System.out.println(part.getContentType());
            System.out.println(body);
            assertEquals("Bonjour", new String(body));
        } else {
            System.out.println(statusCode);
        }
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@测试
public void test()引发ClassNotFoundException{
HttpClient=新的HttpClient();
GetMethod get=新的GetMethod(
"http://DongyangLuo-PC.cn.ibm.com:9080/Cloud_Modeler/datasource/multipart");
试一试{
int statusCode=client.executeMethod(get);
如果(状态代码==200){
//我实例化解析器并尝试实例化
//带有解析器的InMultiPart对象
MultiPartParser=新的MultiPartParser(
get.getResponseBodyAsStream(),“myboundary”);
InMultiPart ins=新的InMultiPart(解析器);
InPart part=ins.next();
System.out.println(“内容类型为:”
+part.getContentType());
InputStream输入=part.getInputStream();
//这种解码将获得java.io.StreamCorruptedException:invalid stream header:48656C6C
字节[]ba=(字节[])新的ObjectInputStream(输入).readObject();
对于(int j=0;j

然后我尝试从输入流中逐个读取字节[]字符,并获得正确的内容。但问题是我不能用这种方法得到像地图一样复杂的物体。

Rekoolno,你得自己试试。当你有了代码,然后陷入困境时,带着一个问题回来。我在问之前已经试过了,只是没有显示出来。你以前试过很好,@Rekoolno。当你问关于SO的问题时,向我们展示你的作品(确保它是SSCCE)。您的编辑是一个很好的改进。我将投票重新开放。非常感谢你的帮助!这几天我正试图根据ApacheWink用户指南解决这个问题。
    @Test
public void test() throws ClassNotFoundException {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(
            "http://DongyangLuo-PC.cn.ibm.com:9080/Cloud_Modeler/datasource/multipart");
    try {
        int statusCode = client.executeMethod(get);
        if (statusCode == 200) {
//            I instantiate the parser and try to instantiate the
//                InMultiPart object with the parser
            MultiPartParser parser = new MultiPartParser(
                    get.getResponseBodyAsStream(), "myboundary");

            InMultiPart ins = new InMultiPart(parser);
            InPart part = ins.next();
            System.out.println("The content type is: "
                    + part.getContentType());
            InputStream input = part.getInputStream();
//              this kind of decoding will get java.io.StreamCorruptedException: invalid stream header: 48656C6C
            byte[] ba = (byte[]) new ObjectInputStream(input).readObject();
            for(int j = 0; j < ba.length; j++){
                System.out.print(ba[j] + "\t");
            }
            System.out.println("The content body is: "
                    + part.getBody(String.class, String.class));
            System.out.println("The content headers are: "
                    + part.getHeaders());
            part = ins.next();
//              this kind of decoding doesn't work because of losing a provider which shouldn't appear in client.
            byte[] body = part.getBody(byte[].class, byte[].class);
            System.out.println(part.getHeaders());
            System.out.println(part.getContentType());
            System.out.println(body);
            assertEquals("Bonjour", new String(body));
        } else {
            System.out.println(statusCode);
        }
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}