How to show file download dialog box in IE 6.0
import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public final class DownloadServlet extends HttpServlet { private static final String basePath = “/doc”;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { String filePath = request.getPathInfo(); String filename = request.getParameter( “filename” ); if( filePath == null && filename != null ) filePath = “/” + filename; if( filename == null ) filename = filePath; if( filename != null ) filename = (new File(filename)).getName();
if( filePath != null ) { InputStream in = null; OutputStream out = null; try { in = getServletContext().getResourceAsStream(basePath + filePath); if( in != null ) { out = new BufferedOutputStream( response.getOutputStream() ); in = new BufferedInputStream( in ); String contentType = “application/unknow”; System.out.println( “contentType: ” + contentType ); response.setHeader(”Content-Disposition”,”attachment; filename=\”" + filename + “\”"); int c; while( ( c=in.read() ) != -1 ) out.write( c ); return; } } finally { if( in != null ) try { in.close(); } catch( Exception e ) {} if( out != null ) try { out.close(); } catch( Exception e ) {} } } response.sendError( HttpServletResponse.SC_NOT_FOUND ); } }
Log in to answer.
BoLaFish 12:25 am on December 30, 2009
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {
private static final String basePath = “/doc”;
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
String filePath = request.getPathInfo();
String filename = request.getParameter( “filename” );
if( filePath == null && filename != null ) filePath = “/” + filename;
if( filename == null ) filename = filePath;
if( filename != null ) filename = (new File(filename)).getName();
if( filePath != null ) {
InputStream in = null;
OutputStream out = null;
try {
in = getServletContext().getResourceAsStream(basePath + filePath);
if( in != null ) {
out = new BufferedOutputStream( response.getOutputStream() );
in = new BufferedInputStream( in );
String contentType = “application/unknow”;
System.out.println( “contentType: ” + contentType );
response.setHeader(”Content-Disposition”,”attachment; filename=\”" + filename + “\”");
int c; while( ( c=in.read() ) != -1 ) out.write( c );
return;
}
} finally {
if( in != null ) try { in.close(); } catch( Exception e ) {}
if( out != null ) try { out.close(); } catch( Exception e ) {}
}
}
response.sendError( HttpServletResponse.SC_NOT_FOUND );
}
}