Hello Friends...
I am new to jsp and i need to edit a .doc file present in a server using jsp!
i tried googling and i got to know about "apache poi".
this is working great for opening a doc file and also for writing a doc file but my problem is i have to edit a doc file and i have to save that file with same name!
but i have two codes one to display doc file in textarea and another one to write one doc file to another one
now i need both at one place that is it has to open a file and it has to edit and save as same as original file!!
here are two codes i have!!
This is for displaying doc file
<%@ page import="java.io.*,java.io.*,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.extractor.WordExtractor" %>
<%
File file = null;
WordExtractor extractor = null ;
String res=null;
try {
file = new File("xyz.doc");
FileInputStream fis=new FileInputStream(file.getAbsolutePath());
HWPFDocument document=new HWPFDocument(fis);
extractor = new WordExtractor(document);
String [] fileData = extractor.getParagraphText();
for(int i=0;i<fileData.length;i++){
if(fileData[i] != null)
res+=fileData[i];
}
%>
<form>
<textarea >
<%out.println(res);%>
</textarea>
<input type="submit" value="submit"/>
</form>
<%
}
catch(Exception exep)
{
out.println(exep.getMessage());
}
%>
This is for Saving one file to another
<%@ page import="java.io.File,java.io.FileInputStream,java.io.FileOutputStream,java.io.IOException,java.io.OutputStream,org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.usermodel.CharacterRun,org.apache.poi.hwpf.usermodel.Range,org.apache.poi.poifs.filesystem.POIFSFileSystem"%>
<%
File file = new File("xyz.doc");
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
CharacterRun run = range.insertAfter("Hello World!!!");
run.setBold(true);
run.setItalic(true);
run.setCapitalized(true);
OutputStream outa = new FileOutputStream(new File("D:/sample2.doc"));
doc.write(outa);
out.println(outa);
out.flush();
out.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}
%>
They both are working perfectly but i need to edit file taken from a file name and to save it again...
please friends help me!!!
thanking u !!!