Solved

Opening File in viewer outside browser

Posted on 2007-03-22
13
603 Views
Last Modified: 2007-10-18
I have a JSP page where I have a hyperlink to view a file. The file could be a word doc, or a pdf doc. If the user clicks on the link the document is opened in the browser. Is there a way to open it outside the browser by launching the corresponding viewer. The browser view should remain the same as it was befoore the link was clicked.

here is the simple JSP (only posting the relevant code)

   <a href="Popup">Click here to download</a>

The servlet that writes the file


public class Popup extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException   {

            String fileName ="XMLAppuserguide.pdf";
        response.setContentType("application/pdf");
       
            ServletOutputStream sos = null;
        sos = response.getOutputStream();
       
            File file = new File(fileName);
            System.out.println("-----------------"+file.getAbsolutePath());
            FileInputStream fis = new FileInputStream(file);

       
            int  b = fis.read();
        while (b!=-1) {
           sos.write(b);
           b=fis.read();
        }
    }
}

0
Comment
Question by:thomas908
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18777963
<a href="1.exe">Click here to download</a>
 
should work
0
 
LVL 8

Author Comment

by:thomas908
ID: 18777984
>><a href="1.exe">Click here to download</a>
I need to do it through a servet, since there is some other logic in servlet that I have not posted.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 18778081
Like this:

<a href="Popup" target="_blank" >Click here to download</a>


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

Expert Comment

by:Zvonko
ID: 18778088
And if you want to open and reuse one single popup window on every link klick, then give the popup window a Name:

<a href="Popup" target="myPop" >Click here to download</a>

0
 
LVL 8

Author Comment

by:thomas908
ID: 18778119
<<<a href="Popup" target="_blank" >Click here to download</a>

But this still opens it in the browser. I want the application to be launched directly.
0
 
LVL 8

Author Comment

by:thomas908
ID: 18778122
To be more precise with what I posted above, there'll be only .tiff images that need to be opened in the microsoft image viewer. There are no pdf and msword files, I posted that just as an example.
0
 
LVL 8

Author Comment

by:thomas908
ID: 18778135
When I give the path to .tif image in my servlet, then I get an open/save dialog box. Is there a way to compress this dialog box, so that the image directly opens in the viewer.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 18778217
try
response.setContentType("application/octet-stream");
response.setContentDisposition("attachment; filename=\""+filename+"\"");
0
 
LVL 8

Author Comment

by:thomas908
ID: 18778422
Getting this error

Abc.java:18: cannot resolve symbol
symbol  : method setContentDisposition (java.lang.String)
location: interface javax.servlet.http.HttpServletResponse
        response.setContentDisposition("attachment; filename=\""+file.getName()+
"\"");
                ^
1 error
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 100 total points
ID: 18780000
response.setHeader("Content-disposition","attachment; filename=\""+file.getName()+"\"");
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 40 total points
ID: 18785749
>> I need to do it through a servet, since there is some other logic in servlet that I have not posted.

Try a download servlet like this:

http://jspwiki.org/wiki/MakingADownloadServlet
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 40 total points
ID: 18790023
well as everybody above has said.. you will have to set the contnet type of your response data depending upon what kind of data you are sending to the client.

for example for a MS-word file you will have to do

response.setContentType("application/msword");
for a gif image it has to be..

response.setContentType("image/gif");

and so on..

you will have to cover a lot of mime types to have it all covered.
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 70 total points
ID: 18791205
Did you try:

            String fileName ="myImage.tif";
        response.setContentType("image/tiff");


0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Table style position to the left 8 26
Hta File displays dynamic File names 4 50
allow link tag to be submit like form 12 17
Diff of the day 2 10
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question