Hi

I am trying to write a servlet that allows users to download ANY
binary file from a web-server. The servlet must operate on either NN
and IE environments.
When I invoke the servlet 'Down' (the code goes within this message.
The file for download in this example is 'winzip70.exe') off-line

http://127.0.0.1:8000/servlet/Down

and click the submit button I get a 'File Download' window informing
that I want to download the file 'Down72d063a0' from 127.0.0.1.
I click 'ok' and a 'Save As' window appears.
I choose where I want to put the file (always referred as
'Down72d063a0' and '.Document' type) and click 'Save'.
An 'Error' window appears informing 'Cannot copy Down72d063a0: cannot
find the specified file. Make sure you specify the correct path and
filename'. I click 'ok'.
An 'Information' window appears informing 'Download complete'.

I have some questions:
why is the download not working?
why is 'winzip70.exe' referred as 'Down72d063a0'?
In the 'Save As' window, why is the file referred as '.Document' type?

Code of '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="submit";
  String myfilename="ficheiro";

  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="+myfilename+"
value='c:/temp/winzip70.exe'></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();
  }
}


I thank you all in advance

===
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