permaculture playing cards*
The moose likes JSP and the fly likes displaying jsp filename to downloading file in internet explorer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "displaying jsp filename to downloading file in internet explorer" Watch "displaying jsp filename to downloading file in internet explorer" New topic
Author

displaying jsp filename to downloading file in internet explorer

grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

i am using the following code to download file from server.


<%
File file = new File("/ed63/ora510/eld611i2comn/html/eisrs/data/backuploadbalance.xlsx");
String resultXLS="excelfileName1441.xlsx";
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
response.setHeader("content-disposition", "attachement; filename=\"" + resultXLS + "\"");
response.setHeader("Content-Transfer-Encoding", "binary");

FileInputStream fileIn = new FileInputStream(file);
OutputStream outExcel = response.getOutputStream();

byte[] outputByte = new byte[8192];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 8192) != -1)
{
outExcel.write(outputByte, 0, 8192);
}

fileIn.close();
outExcel.flush();
outExcel.close();

%>


when i open in firefox the file name shows as "excelfileName1441.xlsx";
in internet explorer the file name shows as jsp file name.



how to display the file name in internet explorer.

Thanks & regards

kumar

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Doing this in a JSP is folly. The first step is to refactor this code into a servlet. Please post back when that is accomplished.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

Thanks for your answer.

i converted to servlet.

code:




import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class down
*/
@WebServlet("/down")
public class down extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public down() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File file = new File("d:\\Client Versions.xlsx");
setResponseHeader(response, "DisplayHideTabs.xlsx",file);
FileInputStream fileIn = new FileInputStream(file);
OutputStream outExcel = response.getOutputStream();

byte[] outputByte = new byte[8192];
//copy binary contect to output stream
while(fileIn.read(outputByte, 0, 8192) != -1)
{
outExcel.write(outputByte, 0, 8192);
}

fileIn.close();
outExcel.flush();
outExcel.close();

}
private void setResponseHeader(HttpServletResponse response, String resultXLS,File file)
{
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
response.setHeader("content-disposition", "attachement; filename=resultXLS.xlsx");
response.setHeader("Content-Transfer-Encoding", "binary");


}

}


i am still facing the problem.

code works fine in firefox.

when it comes to the internet explorer. it displays the filename as jspname when i click open.

i think problem is with response header.

thanks & regards


Kumar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Please UseCodeTags when posting code to the forums.

grk kumar wrote:
when it comes to the internet explorer. it displays the filename as jspname when i click open.


As there is no longer a JSP, what is "jspname"?
grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

Thanks for your suggestion.

here it will displays servlet name when i click open in internet explorer.

it is working fine in firefox.

i am using tomcat server in this case.



Thanks & regards

Kumar



[Thumbnail for error.JPG]

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

I'm guessing that perhaps you mean that part of the requesting URL is being used by IE? It isn't very helpful when you use imprecise phrases like "servlet name". Maybe you could give an example if you don't know the names of things?
grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

the problem is with Internet explorer.

the code works fine chrome and firefox for both 2003 and 2007 format.

when it comes to the internet explorer some thing is missing.

i think i need to set some response header for internet explorer.

any suggestions.

Thanks & regards

Kumar
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Paul Clapham wrote:I'm guessing that perhaps you mean that part of the requesting URL is being used by IE? It isn't very helpful when you use imprecise phrases like "servlet name". Maybe you could give an example if you don't know the names of things?


That WAS a suggestion.
grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

Thanks for your response.

internet explorer using my servlet name as file name.



in this case the file name displays as down.xlsx

can you please give solution for this.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Well, I would suggest you should spell "attachment" correctly. And I would suggest you specify a filename if you are concerned about the filename the browser is going to use. Which it seems you are, since you are complaining about the browser's choice.

As for "down.xslx", that seems like a strange choice for the name of your servlet. Was that really what you meant?
grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

thanks for your response.

i need to force the browser to open the file as given file name.

here the files are changed based on client request.


there are links in jsp page having all filenames.

when user clicks on link the corresponding file downloaded.




this is my requirement.

Thanks & regards

Kumar






grk kumar
Greenhorn

Joined: Jul 18, 2011
Posts: 7
hi

thanks for your response.


excel contains pivot table.

if i use setHeader as


data lost occurred in excel file.

so i must use "inline" in place of "attachment".

thanks & regards

kumar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying jsp filename to downloading file in internet explorer