Hope this helps.
peace,
cedric
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileRetriever extends HttpServlet
{
public void doGet(HttpServletRequest sReq
,HttpServletResponse sRes)
throws IOException,ServletException {
//the choise you give on ur HTML page for the
file to be downloaded
String fileSelected =
sReq.getParameter("fileSelected");
String xxx =
getServletContext().getRealPath("/");
/* after executing the above statement the
string xxx gives the complete path upto the web server directory.Then I am
extending the path by specifying the directory which holds all the downloadable
file*/
String pathOfFile = xxx +
"Downloadablefiles"+"\\" + fileSelected;
File F = new File(pathOfFile);
sRes.setContentType("application/octet-stream");
//the next statement is the most IMPORTANT
statement
sRes.setHeader("Content-Disposition",
"attachment;filename=\"" + fileSelected +"\"");
ServletOutputStream out =
sRes.getOutputStream();
InputStream in = null;
try
{
in = new BufferedInputStream(new
FileInputStream(F));
int ch;
while ((ch = in.read()) !=-1)
{
out.print((char)ch);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
finally
{
if (in != null) in.close(); // very
important
out.close();
}
System.out.println(fileSelected);
}
}
palurumahesh babu wrote:
> sir,
> i want to know how to do File Download Programming in
> Java Servlets. let me know fast. if you have the URL
> of application program , let me know.
>
> Thanking you,
>
> yours
> mahesh/-
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Mail - Free email you can access from anywhere!
> 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
___________________________________________________________________________
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