Open a file for write with a servlet

Posted on 2009-04-30
Last Modified: 2013-11-24
How can you open a file (e.g . a Word file) for write with a servlet. The code below opens it for read.
public void dispatch(HttpServletRequest req,

            HttpServletResponse resp) throws IOException,URISyntaxException {

        String filenameToMakeWriteable = dispatchFile.getAbsolutePath();

        FileInputStream in=new FileInputStream(filenameToMakeWriteable);

        byte[] buffer=new byte[1024];

        int count=0;

        OutputStream out=   resp.getOutputStream();






Open in new window

Question by:grandpal
    LVL 92

    Accepted Solution

    Use a FileOutputStream.
    What is it you are trying to achieve?

    LVL 10

    Expert Comment

    Hava a look here you need a FileOutputStream instead of a FileInputStream:

    Author Comment

    A servlet is called with some args which the servlet interpretes and which leads to the display of a file. The code shown displays the file, but if its is a Word file, for example, you cannot use the save command inside Word to save changes to the file. One possible solution is  return a "file:" - url . I guess I then have to map file so it is accessible  the servlet context root. Any other solution ?
    LVL 92

    Assisted Solution

    not really, if you're saving it with word it needs access to the file


    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 72
    JavaScript parse form with colon separated values 2 48
    countAbc challenge 9 35
    java set up 1 37
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now