• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with opening of doc file.

 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using servlet code to open files by onclicking the file name in my jsp.It works well for png,pdf,text files.But when i am trying to open doc file it is downloading rather than open directly.
Here is my code.Can anyone please help me to solve this.

if(finalFileForOpen != null && finalFileForOpen.trim().length() > 0)
{
file = new File(finalFileForOpen);
int length = 0;
ServletOutputStream outputStream = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(finalFileForOpen);
if(mimetype == null)
{
mimetype = context.getMimeType(finalFileForOpen.toLowerCase());
}
System.out.println(" finalFileForOpen "+finalFileForOpen+" mimetype "+mimetype);
response.setContentType((mimetype != null) ? mimetype : "application/pdf");
response.setContentLength((int) file.length());
response.setHeader("Content-disposition","inline; filename=" + file.getName());
byte[] buffer = new byte[1024];
DataInputStream inputStream = new DataInputStream(new FileInputStream(file));
while((inputStream != null) && ((length = inputStream.read(buffer)) != - 1))
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}

Please help me to solve this.Thanks in advance.
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the content type set for the docs?

And how should it be?
 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan,
For doc files i am getting mimetype as application/msword.I am setting that only as contenttype.
 
bala nannaka
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please change response header from



to and try this
 
SaiManasa Dasari
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi bala,
I have tried that.But if i change inlilne to attachment it will ask to download for even png,txt files also.My requirement is to open file directly by clicking on the file name,not to download it.

Can anyone please help me to solve this.
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it is the browser which behaves when it encounters a response as it pleases.
I can not image how you could influence this besides setting the content type and the content disposition headers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!