File save and downloads instead of opening a web page

Posted on 2006-05-16
Last Modified: 2010-03-31
We have a client who has a log in to a web page that opens a java app when he click on certin hyperlinks. On one computer instead of the app opening it launches a save but that when it save to the local computer it does not open anything it is not even recognized by any program and when opened in notepad it is a buch of garbage. the name of the file is something like viewbriefdocuments
Question by:arahming
    LVL 30

    Expert Comment

    The client machine needs to have Java to be able to run it. If its a .class file, it will not contain plain text-like human-readable characters but it will contain byte-code instructions for the JVM. I think that instead of an application, it better be something like an applet.
    LVL 23

    Accepted Solution


    MIME types :

    Web browsers use MIME types to identify non-HTML files and to determine how to present the data contained in them. Plug-ins can be associated with a MIME type or types, so that when the Web browser downloads a file with that MIME type, the browser also launches the plug-in that handles the file. Other MIME types can be associated with external programs. When the browser downloads files of those MIME types, it launches the appropriate program to view the downloaded file.

    MIME types are useful because they allow Web browsers to handle various file types without having the built-in knowledge. Java servlets can use MIME types to send non-HTML files such as Adobe PDF and Microsoft Word to browsers. Using the proper MIME type helps to ensure that the file gets displayed by the proper plug-in or external viewer. The Resources section provides links to a list of defined MIME types and additional articles on MIME types.

    The MIME type for a PDF file is "application/pdf". To open a PDF file in a servlet, you set the content type in the response header to "application/pdf":
    // MIME type for pdf doc
        res.setContentType( "application/pdf" );  

    To open an Microsoft Word document, you would set the response object's content type to "application/msword" instead of "application/pdf":

        // MIME type for MSWord doc
        res.setContentType( "application/msword" );

    For an Excel document, use the MIME type "application/". In that MIME type, vnd refers to application vendor that must be included to open the file.

    In some cases, the browser doesn't recognize the file's MIME type. That often happens when the required plug-in hasn't been installed for a certain file type. In those cases, the browser will pop up a dialog box, asking the user whether he or she wants to open the file or save it to disk.

    Content disposition
    An HTTP response header named content-disposition allows the servlet to specify information about the file's presentation. Using that header, you can indicate that the content should be opened separately (not actually in the browser) and that it should not be displayed automatically but rather upon some further action by the user. You can also suggest the filename to be used if the content is to be saved to a file. That filename would be the name of the file that appears in the Save As dialog box. If you don't specify the filename, you are likely to get the name of your servlet in that box. To find out more about the content-disposition header, check out Resources.

    In the servlet, you want to set that header as follows:
                      "attachment; filename=" +
                      "Example.pdf" );
        // attachment - since we don't want to open
        // it in the browser, but
        // with Adobe Acrobat, and set the
        // default file name to use.

    If you were opening an Microsoft Word file, you would choose:

                      "attachment; filename" +
                      "Example.doc" );


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now