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 !!!

Recommended Answers

All 3 Replies

hello sir,
i know how to write one .doc file content into another .doc file using poi in jsp but i want to write some text into a .doc file using poi,
so please can anyone help me in doing that
code for writing to one file to another file using jsp is below!!!

<%@ 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:/sample.doc"));
doc.write(outa);
out.println(outa);
out.flush();
out.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}



%>

the above code is used to save file contents of xyz.doc to sample.doc but i need to write a string in that page and i have to store it in sample.jsp ,please help me in doing that!!!
thanking u!!!

1. Just because you didn't get answer in 24 hours you feel like you are entitled to create another new thread?
2. Why are you misusing JSP for something that is not supposed to do? Do document manipulation in servlet and just forward necessary data to page view.

sir thanks for your reply can you help me how to do that i am new to jsp and i didnt started servlets...so please help me!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.