Hi

I'm able to download ANY binary file (for the newbies the code goes
within this message). That's great.

But I can only save the file as '.Document'. How can I solve this? How
can the system understand which type the file is?
The other issue concerns the name of the file. Why isn't the
getParameter(req,myfilename)
read by the system?

Here is the code for 'Down':

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/winzip70.exe";

  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");
    res.setHeader("Content-Disposition","attachment ; filename=" +
getParameter(req,myfilename));
    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();
  }
}

Thanks to all of you
===
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