Name is not same when i download a file using servlet.

Hi,

I have implemented download functionality using servlet. Download is working fine. But, I am not getting the same file name what i am settting in content-disposition value.

oResponse.addHeader("Content-disposition", "attachment; filename=test.csv");

I am getting servlet name as filename without any extension. I am using Apache Tomcat 5.

Any idea ?

[385 byte] By [JK2004a] at [2007-11-27 11:54:45]
# 1

Why did you create a new thread for this rather than continuing on in your existing thread on this exciting topic?

Part of the answer might be that you are giving the wrong content type and confusing the browser. But that's solely based on the unformatted sample of uncompilable drivel posing as code that you posted in your other thread so it's veracity may be challenged.

cotton.ma at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 2

<_<

CeciNEstPasUnProgrammeura at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 3

sorry for that..

JK2004a at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 4

> sorry for that..

Well just continue on here now. The topic is slightly different now but in future when the change is that minor or better when it directly relates to he same problem please stick with one thread.

Now...

Do you see how you are telling the browser it's plain text as a MIME type but then trying to give it a csv extension?

You should try the correct MIME type which would be text/csv

cotton.ma at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 5

I tried changing the Content Type to following types but it does not work:

1. text/comma-separated-values

2. text/csv

3. application/csv

4. application/excel

5. application/vnd.ms-excel

6. application/vnd.msexcel

7. text/anytext

With Content Type=application/vnd-ms-excel extension of the file changed to .xls and it is showing like <servletname>.xls

It is not taking the actual file name (test.csv).

JK2004a at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 6

You are doing something wrong but it is impossible to see what.

Please post your ACTUAL code and be sure to use the code tags when you do (just select your code when you paste it into the message box and then click the code button).

cotton.ma at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 7

// Find the Response in context

HttpServletResponse oResponse = (HttpServletResponse)invocation.getInvocationContext().get(HTTP_RESPONSE);

// Set the content type

oResponse.setContentType(grid.getExporter().getContentType());

// Set the content-disposition aka the file name

oResponse.addHeader("Content-disposition", grid.getExporter().getFilename());

// Set the cache control

setCacheControl( oResponse );

// Get the outputstream

OutputStream oOutput = oResponse.getOutputStream();

// Copy content to output

oOutput.write(grid.getExporter().getContent());

// Flush

oOutput.flush();

}

static void setCacheControl( HttpServletResponse oResponse ) {

// Set a specific header value so that we do not cache exported data from the grid.

// The AddHttpResponseHeaderFilter.java class that is defined by web.xml first sets

// the cache control header and because of that IE does not handle it properly.

// This problem is described in this KB article:

// http://support.microsoft.com/kb/812935

//

// The fix for this is to to not set the "Cache-Control" to max-age to one second

// effectively overwriting the value that is set in the AddHttpResponseHeaderFile.java class

// and allowing IE to open and export the data.

oResponse.setHeader( "Cache-Control", "max-age=1" );

}

JK2004a at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 8

> oResponse.addHeader("Content-disposition", grid.getExporter().getFilename());

This looks different from what you posted first... and especially it does not look like it's setting the attribute even closely correctly.

And your gunk is still not formatted.

http://forum.java.sun.com/help.jspa?sec=formatting

CeciNEstPasUnProgrammeura at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 9

> You are doing something wrong but it is impossible to

> see what.

>

It is in fact quite possible.

The name the browser reports to the launching application is the part of the URL after the last slash but without request parameters. That's usually the name under which the servlet is mapped in the web.xml.

jwentinga at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...
# 10

Which is what the OP is trying to change by using the Content-Disposition header.

This actually works if used as originally posted, but not in the way it is used in the actual code.

@OP: do see the differences between what you posted originally and the actual code you are using?

Just to be safe: the actual name of the header is "Content-Disposition" not "Content-disposition".

Herko_ter_Horsta at 2007-7-29 18:57:50 > top of Java-index,Java Essentials,Java Programming...