简体   繁体   中英

How to read data from StreamingOutput

I have a restful webservice. I am thinking of calling a method from another method in the class. Is it possible? If so, how do I read the content of streaming output in example below? Also, I need both functions separately so that I can return binary files or display text of text files

@Path("/base")
public class Base{

    @GET
    @Path("/base/func1")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response func1(){
        final String some_outfilename = "test.pdf";
        StreamingOutput st = new StreamingOutput(){
            @Override
            public void write(OutputStream os) throws IOException{
                Writer writer = new BufferedWriter(new OutputStreamWriter(os));
                writer.write("test");
                writer.flush();
            }
        };
        return Response.ok(stream, MediaType.APPLICATION_OCTET_STREAM).header("content-disposition","attachment; filename =" + some_outFilename).build();
    }

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    @Path("/base/func2")
    public Response func2(){
        //I want to call func1() from here, like
        //StreamingOutput st = (StreamingOutput)func1().getEntity();
        //But how do I get the "test" written inside??
        //Once obtained I can return it as response
    }
}

EDIT #1: func1() does have a return statement. I put it in later (sorry). func2() is also supposed to have a return statement.. I haven't put it because I do not understand how to structure it.

func1 does not return anything, not even st .

You should move the common code that you want to use from both func1 and func2 to a third private method. This method should return something that can then be wrapped in a StreamingOutput instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM