Download ZIP-file with Internet Exlorer

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();
 
J

jan V

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.

This is a Microsoft problem, not a Java problem. Please go to
www.microsoft.com and locate the appropriate support page/forum. [How I love
all these people struggling with XP's automatic updates... all together now:
what do you get when you misplace your trust in Microsoft? ]
 
A

Andrea Desole

jan said:
This is a Microsoft problem, not a Java problem. Please go to
www.microsoft.com and locate the appropriate support page/forum. [How I love
all these people struggling with XP's automatic updates... all together now:
what do you get when you misplace your trust in Microsoft? ]

you mean explorer is not able to download a file? That would be unusual.
Although, I have to say, the code looks fine.
 
A

Andrew Thompson

...[How I love
all these people struggling with XP's automatic updates... all together now:
what do you get when you misplace your trust in Microsoft? ]

....Did somebody mention XML? ;-)

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"He thinks he can run, it's a matter of pride. But he keeps coming back,
like a cork on the tide."
Paul Kelly 'Deeper Water'
 
R

Roedy Green

But at the client side the popup-windows disappears but NO
file-download dialog comes up.
Possibly in the update some sort of association or mime type list has
got lost or clobbered. I could not find out how IE did its
associations. I figured it must just use Windows for everything or
have some hidden internal list of MIME types.

You might check with other browsers to see if they work, and if
MIMECHECK likes your mime type.

See http://mindprod.com/applets/mimecheck.html
 
J

Joan

tkonrath said:
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
Did you try "allow popups temporarily" in IE popup stopper?
 
D

Dave Glasser

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 encountered this problem recently and the only workaround I found
was to eliminate the popup. I'm guessing that the link the user clicks
to download the file has "target='_blank'" or something similar. If
so, get rid of it, or make it "target=_self". Since your content type
is application/octet-stream, it shouldn't disturb the contents of the
parent window, but rather just bring up the save/open dialog box.

I don't think this will break other browsers, but if it does you may
have to check the browser type when you generate the parent page and
decide then whether or not to use the "target=_blank" attribute.


--

"The first EDSer to see a snake kills it. At GM, the first
thing you do is organize a committee on snakes. Then you
bring in a consultant who knows a lot about snakes. Third
thing you do is talk about it for a year."
--Ross Perot
Business Week,
10/6/86
_______________________________________________________________
 
T

Tris Orendorff

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.

I suspect that your pop-up window is being blocked by ie. Either that, or
ie is not doing the download for security reasons: previously it would
download the file and unzip it and execute it (VIRUS TIME) and now it
doesn't even download your zip file unless you tell it zip files are safe.


--

Sincerely,

Tris Orendorff
[Two antennae meet on a roof, fall in love and get married. The ceremony
wasn't much, but the reception was excellent.]
 
T

tkonrath

Thanks for all the answer. Finally I found a setting option in IE
(Internetoptions -> Security -> Button Customize).
In de Group Download, there is a setting, which deactivates the Save-As
dialog for downloads. I have set this option to Active and everything
works perfect.

Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top