Sure:
package servlets.comum;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import comum.ArquivoGestor;
/**
* Definition of class DownloadFile.
*/
public class DownloadFile extends HttpServlet
{
private String original_filename = "MYFILE.txt";
private String filename="C:\\ABC.txt";
ArquivoGestor arquivoGestor;
/**
* Processes requests for both HTTP >GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
File f = new File(filename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context =
getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );
//
// Set the response and go!
//
response.setContentType( (mimetype != null) ? mimetype :
"application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\""
+ original_filename + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[filename.length()];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
// The lines bellow will retrieve information from request and
// register them into the database (interested user ID/file ID/ and
// licitation ID). The problem, as I said, is that by the time the
// download manager window (that this code displays) shows up the
// entire code has been already executed.
int interessadoID = Integer.parseInt(request.getParameter("int"));
int arquivoID = Integer.parseInt(request.getParameter("arq"));
int licitacaoID = Integer.parseInt(request.getParameter("lic"));
arquivoGestor = new ArquivoGestor();
arquivoGestor.registerLicitacaoDownoad(interessadoID, arquivoID,
licitacaoID);
}
/**
* Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
}
-----Mensagem original-----
De: Martin Gainty
Para: Tomcat Users List
Enviada em: 10/07/2009 22:45
Assunto: RE: RES: Problems downloading files. How to identify the CANCEL
butto n?
yes
Op: could you display the jsp
?
Martin Gainty