1) Use this class is using the jawin package that can be downloaded
here: http://sourceforge.net/projects/jawinproject/
2). Register DLL in your system, launch:
Regsvr32 Html2Rtf.dll
IConverter.java
import org.jawin.*;
public class IConverter
extends DispatchPtr {
public static GUID DIID = new GUID("{1A21ED6F-1BB2-11D9-B234-000D87800515}");
static public final int iidToken;
static {
iidToken = IdentityManager.registerProxy(DIID, IConverter.class);
}
public IConverter() throws COMException {
super();
}
public IConverter(String progid) throws COMException { super(progid);}
// public IConverter(IUnknown other) throws COMException { super(other);}
public IConverter(GUID ClsID) throws COMException { super(ClsID);}
public String ConvertString(String strRtf) throws COMException {
return ( (String) invokeN("ConvertString", new Object[] {strRtf}));
}
public String ConvertFiletoString(String RtfFilePath) throws COMException
{
return ( (String) invokeN("ConvertFiletoString", new Object[]
{RtfFilePath}));
}
public void setPreserveImages(boolean PreserveImages) throws COMException
{
put("PreserveImages", PreserveImages);
}
public void setPreserveFontFace(boolean PreserveFontFace) throws COMException
{
put("PreserveFontFace", PreserveFontFace);
}
public void setPreserveFontSize(boolean PreserveFontSize) throws COMException
{
put("PreserveFontSize", PreserveFontSize);
}
public void setPreserveFontColor(boolean PreserveFontColor) throws
COMException {
put("PreserveFontColor", PreserveFontColor);
}
public void setOutputFormat(int OutputFormat) throws COMException {
put("OutputFormat", OutputFormat);
}
public void setEncoding(int Encoding) throws COMException {
put("Encoding", Encoding);
}
public void setTableBorders(boolean TableBorders) throws COMException
{
put("TableBorders", TableBorders);
}
public void setHtmlParts(int HtmlParts) throws COMException {
put("HtmlParts", HtmlParts);
}
public void setFontFace(String FontFace) throws COMException {
put("FontFace", FontFace);
}
public void setFontSize(String FontSize) throws COMException {
put("FontSize", FontSize);
}
public void setTitle(String Title) throws COMException {
put("Title", Title);
}
public void setFileExtension(String FileExtension) throws COMException
{
put("FileExtension", FileExtension);
}
}
ConvertServlet.java
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import java.io.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jawin.*;
public class ConvertServlet
extends HttpServlet{
public ConvertServlet() {
}
public void init() throws ServletException {
super.init();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
MultipartRequest multi = new MultipartRequest(request,
"/files", 0x1f4000, new DefaultFileRenamePolicy());
Enumeration files = multi.getFileNames();
StringBuffer sb = new StringBuffer();
IConverter iconv = new IConverter(new GUID("{1A21ED6F-1BB2-11D9-B234-000D87800515}"));
iconv.setOutputFormat(1);
iconv.setEncoding(9);
iconv.setPreserveFontFace(false);
iconv.setPreserveFontColor(false);
iconv.setPreserveFontSize(false);
iconv.setPreserveImages(false);
while (files.hasMoreElements()) {
String name = (String) files.nextElement();
File fbin = multi.getFile(name);
FileReader f = new FileReader(fbin.getPath());
int k;
while((k = f.read()) != -1){
sb.append( (char) k);
}
}
String html = iconv.ConvertString(sb.toString());
html = html.replaceAll("charset=iso-8859-1", "charset=iso-8859-15");
System.out.println("ConvertServlet.html: \n" + html);
out.println(html);
}
catch (Exception e) {
e.printStackTrace(out);
}
}
public void destroy() {
}
}