Problem with opening of doc file.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
How is the content type set for the docs?
And how should it be?
And how should it be?
SaiManasa Dasari
Ranch Hand
Posts: 32
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Ivan,
For doc files i am getting mimetype as application/msword.I am setting that only as contenttype.
For doc files i am getting mimetype as application/msword.I am setting that only as contenttype.
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can you please change response header from
to and try this
to and try this
SaiManasa Dasari
Ranch Hand
Posts: 32
posted 12 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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: 1044
6
posted 12 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
I can not image how you could influence this besides setting the content type and the content disposition headers.
| This is my favorite show. And this is my favorite tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |






