• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem with opening of doc file.

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ivan,
For doc files i am getting mimetype as application/msword.I am setting that only as contenttype.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1043
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic