Hi Ronel,

You were very useful helping me in this matter. Thank you very much.

To all of you that wish to write a servlet that download ANY binary
file (in this example I going to download 'cos.zip'), here's the code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.oreilly.servlet.ServletUtils;

public class Down extends HttpServlet {
  String ACTION="action";
  String ACTION_SUBMIT="submeter";
  String myfilename="c:/temp/cos.zip";
  String filename="cos.zip";

  public void init(ServletConfig config) throws ServletException{
    super.init(config);
  }

  public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
    res.setContentType("application/octet-stream; name="+filename+"");
    res.setHeader("Content-Disposition","inline;
filename="+filename+"");
    ServletOutputStream out=res.getOutputStream();

    String uri=req.getRequestURI();
    String action=getParameter(req,ACTION);

    try{
      if((action!=null) && (action.equals(ACTION_SUBMIT))){
        try{
          ServletUtils.returnFile(myfilename,out);
        }
        catch(FileNotFoundException fnfe){}
      }
      else
        createMain(out,uri);
    }
    catch(Exception e){}

    out.flush();
    out.close();
  }

  private String getParameter(HttpServletRequest req, String param){
    String values[]=req.getParameterValues(param);

    if(values!=null)
      param=values[0];

    return param;
  }

  public void createMain(ServletOutputStream out, String uri) throws
IOException{
    out.println("<html>");
      out.println("<HEAD>");
        out.println("<script language='JavaScript'>");
        out.println("<!-- Begin");
        out.println("// End -->");
        out.println("</script>");
      out.println("</HEAD>");
      out.println("<body bgcolor='#FDF9F1'>");
        out.println("<form method=POST action=\""+uri+"\">");
          out.println("<center><table border='0' width='700'>");
            out.println("<tr><td>&nbsp;</td></tr>");
            out.println("<tr>");
              out.println("<td><input type=hidden name='ficheiro'
value="+myfilename+"></td>");
            out.println("</tr>");
            out.println("<tr>");
              out.println("<td><input type=submit name="+ACTION+"
value=\""+ACTION_SUBMIT+"\"></td>");
            out.println("</tr>");
          out.println("</table></center>");
        out.println("</form>");
      out.println("</body>");
    out.println("</html>");

    out.flush();
    out.close();
  }

  public void destroy(){
    super.destroy();
  }
}

===
Joao Carlos Costa







_________________________________________________________
DO YOU YAHOO!?
Get your free @yahoo.com address at http://mail.yahoo.com

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to