Dear friends
u people try this method if u havent tried this earlier
public void downloadFile(String sourceFilePathName)
throws IOException, ServletException, DownloadException {
if(sourceFilePathName == null)
throw new IllegalArgumentException(String.valueOf((new StringBuffer("File '")).append(sourceFilePathName).append("' not found (1040).")));
//start
File[] root = new File("").listRoots();
boolean local = false;
for(int r=0;r<root.length;r++) {
if(sourceFilePathName.toUpperCase().startsWith((root[r]).toString())) {
local = true;
}
}
File srcDirPath = null;
if(local) {
sourceFilePathName = sourceFilePathName;
}else {
sourceFilePathName = config.getServletContext().getRealPath(sourceFilePathName);
}
//end
//if(config.getServletContext().getRealPath(sourceFilePathName) != null)
// sourceFilePathName = config.getServletContext().getRealPath(sourceFilePathName);
File file = new File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[128];
httpResponse.setContentType("application/x-msdownload");
httpResponse.setContentLength((int)(fileLen));
httpResponse.setHeader("Content-Disposition", "attachment; filename=".concat(String.valueOf(getLocalFileName(sourceFilePathName))));
String xxx="Content-Length: "+fileLen+"\r\n";
try{
while((long)totalRead < fileLen)
{
if(fileLen-totalRead > 128)
{
readBytes = fileIn.read(b, 0, 128);
totalRead += readBytes;
}
else
{ readBytes=fileIn.read(b,0,(int)fileLen-totalRead);
totalRead= totalRead+readBytes; }
httpResponse.getOutputStream().write(b, 0, readBytes);
}
fileIn.close();
//httpResponse.getOutputStream().close();
}catch(java.net.ProtocolException e){
//System.out.println("hai10 "+e);
}
}
Hope This will work for u as i have tried and tested to comeout successfully
n'joy
Originally posted by Alex Vasconcelos:
Hello all,
Has anyone been able to successfully write servlet code to download files to a client machine that works with both Netscape and IE ?
Currently I have code that generates a file to be downloaded and streams that file back to the client. This works perfectly with Netscape:
response.reset();
response.setContentLength(size);
response.setContentType("application/zip; name=\"" + fileName + "\";");
response.setHeader("Content-Disposition" , "attachment;filename=" + fileName);
response.getOutputStream().write(file);
response.getOutputStream().flush();
response.getOutputStream().close();
With IE I get an error trying to display the page. To make it work with IE all I have to do is remove the line where I set the Content-Disposition header, but this makes IE try to save the file as the name of the servlet. In other words, if the link is http://www.foo.com/aServlet, IE will try to save a file called "aServlet", which is not what I want.
According to RFC's the Content-Disposition hearder is supposed to take care of that, but there seems to be several problems with the way IE handles that.
I am currently using IE 5.5 SP 2. Have also tried this with IE 6.0 and it didn't work.
Any help will be greatly appreciated.
Thanks in advance,
Alex V.