Java Sample
When you wish to edit
and save your document using Zoho Writer's editor to your remote
server, you need to do a form submit from your application to Zoho
Writer as mentioned here.
The response for this form will be a
Zoho Writer editor with the content of the document loaded. After
editing your document, when you press save, Zoho Writer converts the
document to the required format(doc/pdf/sxw...etc.,) specified in the
above form and sends the modified content to the saveurl.
Assume your saveurl is - http://yourwebsite.com/SaveDoc.do
A sample action (SaveDoc.do) written using Struts Application that will parse, retrieve and save the content sent from Writer to your local server will look like the one mentioned below :
import
org.apache.struts.action.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletInputStream;
import
java.util.Hashtable;
import java.io.*;
import
org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
public class
MySaveService extends Action {
public ActionForward execute
(ActionMapping actionMap, ActionForm actionForm, HttpServletRequest httpReq,
HttpServletResponse response) throws Exception {
OutputStream out=null;
response.setCharacterEncoding("UTF-8");
out=response.getOutputStream();
DiskFileUpload upload =
new DiskFileUpload();
upload.setSizeMax(-1);
java.util.List list =
upload.parseRequest(httpReq);
java.util.Iterator items =
list.iterator();
String docName = null;
byte[] htmlStrInBytes = null;
String id = null;
String format = "doc";
String url=null;
String filename=null;
while (items.hasNext()) {
FileItem item = (FileItem) items.next();
if (item.getFieldName().equalsIgnoreCase("content"))
{
htmlStrInBytes = item.get(); //The
modified content sent by the zohowriter service is saved in this variable
htmlStrInBytes
}
if (item.getFieldName().equalsIgnoreCase("format"))
{
byte[] fmt = item.get();
format = new String(fmt);
}
if
(item.getFieldName().equalsIgnoreCase("id"))
{
byte[] id1 = item.get();
id = new String(id1);
}
if (item.getFieldName().equalsIgnoreCase("filename"))
{
byte[] filename1 = item.get();
filename = new String(filename1);
}
}
int tmp=filename.indexOf(".");
if(tmp>=0)
{
filename=filename.substring(0,tmp)+"."+format;
}
String path ;//Specify the path where the
document is to be stored
File f = new File(path);
FileOutputStream fos = new
FileOutputStream(f);
if(f.exists())
{
fos.write(htmlStrInBytes);//Writing the
content into the file
}
fos.close();
return null;
}
}