Link to home
Start Free TrialLog in
Avatar of grandpal
grandpal

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hava a look here you need a FileOutputStream instead of a FileInputStream: http://www.javacoffeebreak.com/java103/java103.html
Avatar of grandpal
grandpal

ASKER

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 ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial