Hi Ronel,

I tried your suggestions unsuccessfully.

First issue:
You were right. I want to make my servlet flexible; that is to say,
make it smart enough to set the Content-Type based on the file
extension. To me this means that it should exist a type that works
with all kinds of files. You see, I wrote a servlet for upload,
different from Jason Hunter's one, that works with
'multipart/form-data' Content-Type, allowing me to upload ANY binary
file. Why a similar type does not exist for download?
The point is not to write servlets for each type of file. Imagine that
you put in a Web-page 40 different types of files for download. Do you
have to write 40 different servlets (i.e. 40 different Content-Types)
to process each type? That's not economic.

Second issue:
When I put the value of ficheiro to be just the name of the file
("winzip70.exe") and not the absolute path to the file, it cannot find
the file, i.e. the download won't occur.

Situation updated:
I can only save the file as '.Document'.
The name of the file is 'Downf83f2156' instead of 'winzip70'.

I am sure that my mistake is basic, but I read so many times the code
that I became imune to it just like a blind guy. Any help is very
appreciated.
Thanks.

The code of 'Down' follows:

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"; //if I put "winzip70.exe"
the download won't occur

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

  public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
    String filename=req.getParameter("ficheiro");

    res.setContentType("application/octet-stream;
name=\""+filename+"\"");
    res.setHeader("Content-Disposition","inline;
filename=\""+filename+"\""); // I used o have 'attachment' instead of
'inline' but the results remains the same - I can only save the file
as '.Document'
    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> </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