Open a file for write with a servlet

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();
        while((count=in.read(buffer))!=-1){
            out.write(buffer);
        }
    
    }

Open in new window

grandpalAsked:
Who is Participating?
 
objectsCommented:
Use a FileOutputStream.
What is it you are trying to achieve?

0
 
mahomeCommented:
Hava a look here you need a FileOutputStream instead of a FileInputStream: http://www.javacoffeebreak.com/java103/java103.html
0
 
grandpalAuthor Commented:
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 ?
0
 
objectsCommented:
not really, if you're saving it with word it needs access to the file

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.