T
tkonrath
Hi ...
I have written a Servlet, which starts in a popup-window and it just
reads a file (in my case a zip-file) into a byte - array and sends it
to a popup-window for downloading. The popup-window disappears and the
file-download dialog from the internet explorer appears.
This all worked fine since my Windows XP has updated the Internet
Explorer to Version 6.0.2900.2180.xpsp_sp2_gdr.050301-1519 it doesn't
work.
The Servlet still works fine. It still reads the file into a byte array
and sends it to the popup-window (I debug every step and no error
occurs).
But at the client side the popup-windows disappears but NO
file-download dialog comes up.
I have tried the same thing with an other Version of the Internet
Explorer (6.0.2800.1106). There, everything works fine and the
file-download dialog is shown.
Pleas, can anyone help me ... THANKS.
Tom
My Download Servlet:
/**
* The DownloadServlet retrieves a file from the file system, the
location
* of which was either put into the session or is taken from a request
* parameter.
*/
public class DownloadServlet extends HttpServlet {
/**
* maps file extensions to content types.
* Note that extensions must be lower case in order to
* be recognized properly.
*/
private static final String CONTENT_TYPES[][] = {
{".pdf", "application/pdf"},
{".doc", "application/msword"},
{".rtf", "application/msword"},
{".pxml", "text/xml"},
{".xml", "text/xml"},
{".xls", "application/msexcel"},
{".txt", "text/plain"},
{".html", "text/html"},
{".htm", "text/html"},
{".csv", "application/msexcel"},
{".gif", "image/gif"},
{".tiff", "image/tiff"},
{".tif", "image/tiff"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".zip", "application/zip"}
};
/**
* The map generated from CONTENT_TYPES
*/
private static Map CONTENT_TYPE_MAP = new HashMap();
static {
for(int i = 0; i < CONTENT_TYPES.length; ++i) {
CONTENT_TYPE_MAP.put(CONTENT_TYPES[ i ][0], CONTENT_TYPES[ i ][1]);
}
}
/** Reference to the class logger */
private static Logger log = Logger.getLogger(
DownloadServlet.class.getName());
/** Field for making the revision of the class available */
public static final String revision = "$Revision: 2.19 $";
/**
* @see
javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
doGet(req,res);
}
/**
* @see
javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Object fileForDownload = session.getAttribute("fileForDownload");
this.downloadFile(request, response);
}
private void downloadFile (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
HttpSession session = request.getSession();
File file = (File) session.getAttribute("fileForDownload");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
if(file == null) {
throw new SecurityException("No file found for download.");
}
String fileName = file.getName();
String extension = FileUtil.getExtension(file.getName());
log.debug("Downloading " + file);
if(CONTENT_TYPE_MAP.containsKey(extension)) {
response.setContentType(
(String)CONTENT_TYPE_MAP.get(extension.toLowerCase()));
} else {
response.setContentType("application/octet-stream");
}
response.addHeader(
"Content-Disposition",
"attachment; filename=" + file.getName());
byte[] docfile = loadFile(file.getPath());
response.setContentLength(docfile.length) ;
response.getOutputStream().write(docfile);
response.getOutputStream().flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServletException(e.getMessage());
} finally {
//file.delete();
session.setAttribute("fileForDownload", null);
}
}
/**
* Reads the file and returns its content as a byte array
* @param FileName
* @return file as byte array
* @throws IOException
*/
public static byte[] loadFile(String FileName) throws IOException {
FileInputStream fis = null;
FileChannel fcin = null;
BufferedInputStream fileInBuf = null;
try {
if(FileName == null) {
return new byte[0];
}
FileInputStream fileIn = new FileInputStream (FileName);
fileInBuf = new BufferedInputStream(fileIn);
fis = new FileInputStream(FileName);
final int BUF_SIZE = 4096;
byte buf[] = new byte[BUF_SIZE];
int length = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((length = fileInBuf.read(buf)) > 0) {
baos.write(buf, 0, length);
}
return baos.toByteArray();
} catch (IOException e) {
throw e;
} finally {
try {
if(fcin != null) {
fcin.close();
}
if(fis != null) {
fis.close();
}
if(fileInBuf != null) {
fileInBuf.close();
I have written a Servlet, which starts in a popup-window and it just
reads a file (in my case a zip-file) into a byte - array and sends it
to a popup-window for downloading. The popup-window disappears and the
file-download dialog from the internet explorer appears.
This all worked fine since my Windows XP has updated the Internet
Explorer to Version 6.0.2900.2180.xpsp_sp2_gdr.050301-1519 it doesn't
work.
The Servlet still works fine. It still reads the file into a byte array
and sends it to the popup-window (I debug every step and no error
occurs).
But at the client side the popup-windows disappears but NO
file-download dialog comes up.
I have tried the same thing with an other Version of the Internet
Explorer (6.0.2800.1106). There, everything works fine and the
file-download dialog is shown.
Pleas, can anyone help me ... THANKS.
Tom
My Download Servlet:
/**
* The DownloadServlet retrieves a file from the file system, the
location
* of which was either put into the session or is taken from a request
* parameter.
*/
public class DownloadServlet extends HttpServlet {
/**
* maps file extensions to content types.
* Note that extensions must be lower case in order to
* be recognized properly.
*/
private static final String CONTENT_TYPES[][] = {
{".pdf", "application/pdf"},
{".doc", "application/msword"},
{".rtf", "application/msword"},
{".pxml", "text/xml"},
{".xml", "text/xml"},
{".xls", "application/msexcel"},
{".txt", "text/plain"},
{".html", "text/html"},
{".htm", "text/html"},
{".csv", "application/msexcel"},
{".gif", "image/gif"},
{".tiff", "image/tiff"},
{".tif", "image/tiff"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".zip", "application/zip"}
};
/**
* The map generated from CONTENT_TYPES
*/
private static Map CONTENT_TYPE_MAP = new HashMap();
static {
for(int i = 0; i < CONTENT_TYPES.length; ++i) {
CONTENT_TYPE_MAP.put(CONTENT_TYPES[ i ][0], CONTENT_TYPES[ i ][1]);
}
}
/** Reference to the class logger */
private static Logger log = Logger.getLogger(
DownloadServlet.class.getName());
/** Field for making the revision of the class available */
public static final String revision = "$Revision: 2.19 $";
/**
* @see
javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
doGet(req,res);
}
/**
* @see
javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Object fileForDownload = session.getAttribute("fileForDownload");
this.downloadFile(request, response);
}
private void downloadFile (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
HttpSession session = request.getSession();
File file = (File) session.getAttribute("fileForDownload");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
if(file == null) {
throw new SecurityException("No file found for download.");
}
String fileName = file.getName();
String extension = FileUtil.getExtension(file.getName());
log.debug("Downloading " + file);
if(CONTENT_TYPE_MAP.containsKey(extension)) {
response.setContentType(
(String)CONTENT_TYPE_MAP.get(extension.toLowerCase()));
} else {
response.setContentType("application/octet-stream");
}
response.addHeader(
"Content-Disposition",
"attachment; filename=" + file.getName());
byte[] docfile = loadFile(file.getPath());
response.setContentLength(docfile.length) ;
response.getOutputStream().write(docfile);
response.getOutputStream().flush();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServletException(e.getMessage());
} finally {
//file.delete();
session.setAttribute("fileForDownload", null);
}
}
/**
* Reads the file and returns its content as a byte array
* @param FileName
* @return file as byte array
* @throws IOException
*/
public static byte[] loadFile(String FileName) throws IOException {
FileInputStream fis = null;
FileChannel fcin = null;
BufferedInputStream fileInBuf = null;
try {
if(FileName == null) {
return new byte[0];
}
FileInputStream fileIn = new FileInputStream (FileName);
fileInBuf = new BufferedInputStream(fileIn);
fis = new FileInputStream(FileName);
final int BUF_SIZE = 4096;
byte buf[] = new byte[BUF_SIZE];
int length = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((length = fileInBuf.read(buf)) > 0) {
baos.write(buf, 0, length);
}
return baos.toByteArray();
} catch (IOException e) {
throw e;
} finally {
try {
if(fcin != null) {
fcin.close();
}
if(fis != null) {
fis.close();
}
if(fileInBuf != null) {
fileInBuf.close();