How to open a ".doc" file with ms word directly with this servlet?

Here is a servlet for opening a word or a excel or a powerpoint or a pdf file,but I don't want the "file download" dialog appear,eg:when i using this servlet to open a word file,i want open the ".doc" file with ms word directly,not in IE or save.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class OpenWord extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
String strFileName = req.getParameter("filename");
int len = 0;
String strFileType1 = "application/msword";
String strFileType2 = "application/vnd.ms-excel";
String strFileType3 = "application/vnd.ms-powerpoint";
String strFileType4 = "application/pdf";
String strFileType = "";
if(strFileName != null) {
     len = strFileName.length();
     if(strFileName.substring(len-3,len).equalsIgnoreCase("doc")) {
          strFileType = strFileType1;
     } else if(strFileName.substring(len-3,len).equalsIgnoreCase("xls")) {
          strFileType = strFileType2;
     } else if(strFileName.substring(len-3,len).equalsIgnoreCase("ppt")) {
          strFileType = strFileType3;
     } else if(strFileName.substring(len-3,len).equalsIgnoreCase("pdf")) {
          strFileType = strFileType4;
     } else {
          strFileType = strFileType1;
if(strFileName != null) {
     ServletOutputStream out = res.getOutputStream();
     res.setContentType(strFileType); // MIME type for word doc
//if uncomment below sentence,the "file download" dialog will appear twice.
     //res.setHeader("Content-disposition","attachment;filename="+strFileName);
     BufferedInputStream bis = null;
     BufferedOutputStream bos = null;
     String path = "d:\\"; //put a word or a excel file here,eg a.doc
     try {
     File f = new File(path.concat(strFileName));
     FileInputStream fis = new FileInputStream(f);
     bis = new BufferedInputStream(fis);
     bos = new BufferedOutputStream(out);
     byte[] buff = new byte[2048];
     int bytesRead;
     while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
     bos.write(buff, 0, bytesRead);
     } catch(NullPointerException e) {
     System.out.println ( "NullPointerException." );
     throw e;
     } catch(FileNotFoundException e) {
     System.out.println ( "FileNotFoundException." );
     throw e;
     } catch(final IOException e) {
     System.out.println ( "IOException." );
     throw e;
     } finally {
     if (bis != null)
     bis.close();
     if (bos != null)
     bos.close();

Hello!
Does some one of you had open a MS word file (.doc) in Java search for a token like [aToken] replace it with another text and then feed it to a stream of save it?
I want to build a servlet to open a well formatted and rich on media (images) ms word document search for tokens and replace them with information form a web form.
Any Ideas?
Thank you in advanced.

Similar Messages

  • How to open a .DOC file with MS-Word ?

    Hye fellow Java freaks,
    I have made an application to upload .DOC and .RTF files to the server using servlets and the Jakarta Commons FileUpload package.
    The files are uploading successfully.
    Now, I want to place a link to the most recently uploaded file and then, on clicking that link, I want the file to open not in the browser, rather with MS-Word.
    What is the servlet code that I should implement to accomplish this ?
    Any suggesions ?
    Thanx in advance.

    I think this is a client-side issue - how the browser chooses to deal with the .doc file it receives.
    You can 'save as' and then open the file by hand.
    --Jon                                                                                                                                                                                                                                                                                                                               

  • How to open a .doc file in MS word?

    Hello
    Can any body direct me to any api or library or sample, where by if you have word document in your local machine, you can run a java program to open that file in MS word. Basically Microsoft word is opened with the local file in your machine and displayed to the user.
    I looked for a while on this, could find anything...
    Your help is really appreciated.
    Thanks
    Jeevan

    >
    Can any body direct me to any api or library or sample, where by if you have word document in your local machine, you can run a java program to open that file in MS word. Basically Microsoft word is opened with the local file in your machine and displayed to the user.>[Desktop.open(File)|http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html#open(java.io.File)]. (Note that here - a word Doc would be opened in Open Office - but that is more useful since Word is not installed.)
    >
    Your help is really appreciated.>For my part, your thanks are best expressed by assigning [Duke stars|http://wikis.sun.com/display/SunForums/Duke+Stars+Program+Overview] to the answer, and marking it as 'correct' or 'helpful'.

  • How to open a doc file using jsp Anchor tag

    when i am trying to open a doc file using a jsp it is opening with out proper alignment.
              how to open a doc file with proper alignment using Anchor Tag in JSp Page
              

    Hello!
    Does some one of you had open a MS word file (.doc) in Java search for a token like [aToken] replace it with another text and then feed it to a stream of save it?
    I want to build a servlet to open a well formatted and rich on media (images) ms word document search for tokens and replace them with information form a web form.
    Any Ideas?
    Thank you in advanced.

  • I recently updated my OS to OS X. I had to update my word and powerpoint etc... to 2011 version, but now when I want to open a .doc file made in word 2003 it removes all the text in it! I did not back anything up!

    I recently updated my OS to OS X. I had to update my word and powerpoint etc... to 2011 version, but now when I want to open a .doc file made in word 2003 it removes all the text in it! I did not back anything up! All my mums work is on there for her job and now she cant use it! I need help fast!

    If you updated from 2003 that must be on a Windows machine, can you recover them from there?

  • Firefox keeps aksing me what programs that i want to open a doc.file with every time i try to download a doc file.

    the program that I use is open office. I chekced the box that says that this will always open a doc file but it keeps asking me about it.

    Firefox should be able to remember your preference -- as long as the site is specifically identifying the file as a distinct content type. Some sites may fail to do this and simply indicate it is a binary file that the browser can't handle, in which case Firefox doesn't store a specific preference.
    So this could be a problem caused by how the server is identifying the content type. Or it could be a problem with the settings file that stores download preferences. The workaround for that problem is to remove the file and have Firefox rebuild it. If you have changed settings from the default (for example, how to view PDFs) you will have to change those settings again.
    If you want to try it:
    Open your current Firefox settings (AKA Firefox profile) folder using
    Help > Troubleshooting Information > (in the first table) "Show in Finder" button
    Leaving that window open, switch back to Firefox and Exit/Quit
    Pause while Firefox finishes its cleanup, then rename '''mimeTypes.rdf''' to something like mimeTypes.old
    Restart Firefox and see whether its memory improves.

  • How to Open a rar file with downloaded software

    I was bothering this forum yesterday trying to find some way to get a file opened that I had downloaded. Today I discovered that my problem was that "rar" files need their own software to enable them to be opened....So I downloaded the software UnRarX...and it is now sitting on the bottom of my desktop...but I can not get the software into the Applications folder, and so I can not figure out how to use this little package of software to open my "rar" files...
    What do I have to do to open my files with this new software???
    Thanks..
    Miguel

    HI,
    The UnRarX application should be accessible from your Dock after installing the software. The application icon looks like this:
    "How to extract a rar archive?"
    1. Launch UnRarX.
    2. Drag rar archive into the UnRarX window.
    3. Extraction begins automatically.
    Carolyn

  • How to open a DWG file with AutoCAD options (AppleScript)?

    Hi,
    I'm looking for help specifying the AutoCAD options when opening DWG files.  I have DWG files of floor plans that I always want to open at the same scale. 
    I can successfully open a DWG file with the default options using:
    open POSIX file "/Users/crmckinnon/Desktop/cabin_floor_plan.dwg" as alias without dialogs
    But I don't want the default options, so if I try to specify them:
    open POSIX file "/Users/crmckinnon/Desktop/cabin_floor_plan.dwg" as alias with options {class:AutoCAD options, scale unit:"autocad inches"} without dialogs
    I get a "Can't find alias" error.  How do I specify the AutoCAD options?  I'm sure this is due to my lack of understanding of AppleScript.  Any help would be appreciated!
    Thanks,
    Chris

    I'll answer my own question.  Here's how I specified the options:
    open POSIX file filePath as alias with options {pGSO:original size, pASU:inches, pASR:12.0 / 0.1875} without dialogs
    I had to look in the Apple Script dictionary to get the "codes" to use.  The variable names didn't work.  I think that's because some have "global" in the name which is a keyword in Apple Script.  Here's what the codes translate to:
    pGSO = "global scale options"
    pASU = "scale unit"
    pASR = "scale ratio"
    Thanks,
    Chris

  • "uses a file type that is blocked from opening in this version" error message when opening a *.doc file with Word already running

    Several customers running different versions of Office 2011 (14.4.1-14.4.5) on OSX varying from 10.7.5 to 10.9.5, running on various kinds of hardware (iMac/MacBook Pro/MacBook Air) of various ages are having issues opening *.doc files if the Word is already open. The error message that gets displayed is, "XXXX.doc uses a file type that is blocked from opening in this version"
    When the customer tries to open the same file via File-Open, she gets "The file is locked for editing. you can open the file as read-only".
    When trying to do so, she gets "Word cannot open this document. The document might be in use, the document might not be a valid Word document, or the file name might contain invalid characters".
    If Word gets Force-quit, the same document opens without any problems.
    1. Repairing Disk permissions was ran several times. and the volume was found to be OK.
    2. I have noticed that in this scenario either deleting the normal.dotm or com.microsoft.word.plist (~/Library/Preferences) sometimes resolves the issue, sometimes it doesn’t. There is no pattern to follow. All versions of Office are affected, the fully updated and the non-updated ones.
    3. I have tried completely removing the suite using Office 2011 Uninstall.app and/or Remove Office 2011 Uninstaller.pkg, then going through customer's library and manually removing all the Office references.
    4. None of these systems had Office 2008 in the past.
    Any help will be greatly appreciated.

    Reboot both the Mac and the server. Word opens .DOC files and Excel opens .XLS files. As you have found out by copying the files to the computer. It is the connection between the 2 computer that is causing the error.

  • How to open a jpg file with Adobe Camera Raw

    I know it is possible to open a jpg file with Adobe Camera Raw using Bridge but is it possible to do this using PSE7?  Thanks.

    Yes. In the editor, File>Open As, and choose camera raw as the format. John Ellis also has a script for doing this directly from the organizer, but I don't have the link right at the moment. Hopefully he'll be along with it sooner or later.

  • Warning when opening a .doc file (with Pages) ruins my applescript convert process

    I have 2000 plus documents (some Pages, most .doc) that I need to convert to PDF. I've got a working applescript droplet that converts both doc and pages files very nicely... but on some of the .doc files I get this dialog: "Some warnings occured. Would you like to review them now."  This of course, ruins my script run.
    Is there a way to suppress this warning when Pages opens a .doc file?

    Only if there is a script for dialog boxes in your AppleScript which you would have to tailor for the responses.
    You can record snippets of mouse activity using the Script Editor and include those in your script if there are no AppleScript library items.
    Peter

  • How to open an excel file with password in Crystal Reports XI  Release 2

    I have a problem when i try to open an excel 2003 file with password that i know
    i recevived an error code 0xc59
    How can I open a file in excel 2003 protected by password to create the report?
    Thanks in advanced

    Hi Pierluigi,
    It looks like you have to open the excel file and use the data.
    A few informative links.
    http://support.microsoft.com/kb/257819#RetrieveExcel
    http://support.microsoft.com/kb/211378/EN-US/
    Hope this helps.
    Regards,
    Abhijeet Taskar.

  • Unable to open skydrive docs files with Lumia

    Hello, I can open  doc files from skydrive with PC and browser, but not with Lumia phone office application. There is text : "there were some problems opening this file, some part may be missing. You may correct the problem opening and fixing the problem with PC"  I opened original and skydrive saved files. They were working normally with Openoffice or with browser but not with lumia. Thank you for your help. 
    Solved!
    Go to Solution.

    OpenOffice is known to not write fully Office compliant files. It may well be that is you open in Office and then save as a new file it will load up fine.   OpenOffice != Office and do not let them make you believe it is !
    Click on the blue Star Icon below if my advice has helped you or press the 'Accept As Solution' link if I solved your problem..

  • How to open a .MOV file with Maverick?

    Hello,
    I  recorded some videos with my iPhone 4S. i sync it then with my computer, but it says that it can not open them. Why? How can i open .MOV files with my computer (running with Maverick 10.9.4) ?

    Try VLC.
    Video Player - VLC
    More.
    Video Player - Divx
    Video Player – Flip4Mac

  • How to open a ai file with some layers named in Japanese with a AI CS5(English Ver)

    I can't open some ai files that may contains some layers named in Japanese. I am using AI CS5 (Eng Ver) and how can I open them?
    Thx!

    At worst you should be getting garbage substituted characters just like when you don't have specific fonts on text objects, but otherwise you should be able to open the file. The only thing that could prevent this otehrwise is something systemic, so you may need to install support for Asian languages (in particular if you are still on Windows XP) or even switch the UI language.
    Mylenium

Maybe you are looking for