0

I saw this link

How do I use getOutputStream() and getWriter() in the same servlet request?

and this link

best practice response.getOutputStream

I have tried something but can not succeed so far.

My doGet method

public void doGet(HttpServletRequest req, HttpServletResponse res){
this.doPost(req,res);
}

My doPost method:

public void doPost(HttpServletRequest req, HttpServletResponse res){
   ServletOutputStream sos = response.getOutputStream();
   PrintWriter out = new PrintWriter(new OutputStreamWriter(sos, "utf-8"));


if(something happens){
out.println(string_specific_to_situation);
 return;
}
else if(some other thing happens){

foo(sos,other_string_specific_to_situation);
out.println(blabla);
 return;
}
else if(some thing else happens){

foo(sos,else_string);
out.println(dotdot);
return;
}


}

A foo method

public void foo(ServletOutputStream sos,String str){

int                 length   = 0;
    InputStream is =null;
    is= new ByteArrayInputStream(str.getBytes("UTF-8"));


    //
    //  Set the response and go!
    //
    //
    response.setContentType( "application/octet-stream" );
    response.setContentLength( (int)str.length() );
    response.setHeader( "Content-Disposition", "attachment; filename=\"" + name + "\"" );

    //
    //  Stream to the requester.
    //
    byte[] bbuf = new byte[20];
    DataInputStream in = new DataInputStream(is);

    while ((in != null) && ((length = in.read(bbuf)) != -1))
    {
        sos.write(bbuf,0,length);
    }

    in.close();
    sos.flush();
    sos.close();
    return;

}

As you can see, i want to make user side download a file, also i want to be able to show a message,

is not it possible?

Thanks

Community
  • 1
  • 1
merveotesi
  • 2,145
  • 15
  • 53
  • 84

1 Answers1

0

This is certainly possible. However, the first thing you probably want to do is redesign this so that you're not passing your entire payload through a String - especially not if your data type is really an octet-stream, which a String will not reliably hold.

I am confused by "want to be able to show a message" - show a message to the web client, or show a message on the console? If you also want to stream to the console, when you call sos.write(bbuf, 0, length), also immediately call System.out.write(bbuf, 0, length) (for example) - and you will output to both locations.

On the other hand, if you are trying to send the web client a file and a message at the same time, this is not possible. You would first need to send the user the "message" - probably a text/html response - containing a link back to the servlet that allows the user to download the file contents. (You will see that many sites use JavaScript or a meta refresh tag within the HTML to cue the browser to attempt an automatic download, immediately or shortly prompting the user to essentially click this download link.)

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • Thanks for reply, second choice i want to send message to client. If i choose to show a link to user to download the file, how can i pass the string(download file content) as a post parameter? – merveotesi Dec 02 '11 at 15:33
  • tuxi - don't. Retrieve or generate the content when they request the download after viewing the message instead. – ziesemer Dec 02 '11 at 15:52