I am new to domino java. I am trying to implement ajax file upload feature.
For that, I have created two java classes in the domino agent.
1)FileUploadServlet
2)FileUploadListener
In the agent properties, I have set “Base class” property to “fileupload.FileUploadServlet.class”
When I call the agent, I get “Error loading agent base class”
Below is the code of both the files.
Please help.
FileUploadServlet.java**
package fileupload;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import javax.servlet.ServletException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import lotus.notes.*;
public class FileUploadServlet extends HttpServlet implements Servlet {
private static final long serialVersionUID = 2740693677625051632L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
NotesThread.sinitThread();
HttpSession session = request.getSession();
FileUploadListener listener = null;
StringBuffer buffy = new StringBuffer();
long bytesRead = 0, contentLength = 0;
PrintWriter out1 = response.getWriter();
out1.println("GEt method called");
if (session == null){
return;
} else if (session != null){
listener = (FileUploadListener)session.getAttribute("LISTENER");
if (listener == null){
return;
} else {
bytesRead = listener.getBytesRead();
contentLength = listener.getContentLength();
}
}
response.setContentType("text/xml");
buffy.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
buffy.append("<response>\n");
buffy.append("\t<bytes_read>" + bytesRead + "</bytes_read>\n");
buffy.append("\t<content_length>" + contentLength + "</content_length>\n");
if (bytesRead == contentLength) {
buffy.append("\t<finished />\n");
session.setAttribute("LISTENER", null);
} else {
long percentComplete = ((100 * bytesRead) / contentLength);
buffy.append("\t<percent_complete>" + percentComplete + "</percent_complete>\n");
}
buffy.append("</response>\n");
out.println(buffy.toString());
out.flush();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Post method called");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
FileUploadListener listener = new FileUploadListener();
HttpSession session = request.getSession();
session.setAttribute("LISTENER", listener);
upload.setProgressListener(listener);
List uploadedItems = null;
FileItem fileItem = null;
String filePath = "c:\\temp";
try {
uploadedItems = upload.parseRequest(request);
Iterator i = uploadedItems.iterator();
while (i.hasNext()) {
fileItem = (FileItem) i.next();
if (fileItem.isFormField() == false) {
if (fileItem.getSize() > 0) {
File uploadedFile = null;
String myFullFileName = fileItem.getName(), myFileName = "", slashType = (myFullFileName.lastIndexOf("\\") > 0) ? "\\" : "/";
int startIndex = myFullFileName.lastIndexOf(slashType);
myFileName = myFullFileName.substring(startIndex + 1, myFullFileName.length());
uploadedFile = new File(filePath, myFileName);
fileItem.write(uploadedFile);
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileUploadListener.java**package fileupload;
import org.apache.commons.fileupload.ProgressListener;
public class FileUploadListener implements ProgressListener{
private volatile long bytesRead = 0L, contentLength = 0L, item = 0L;
public FileUploadListener() {
super();
}
public void update(long aBytesRead, long aContentLength, int anItem) {
bytesRead = aBytesRead;
contentLength = aContentLength;
item = anItem;
}
public long getBytesRead() {
return bytesRead;
}
public long getContentLength() {
return contentLength;
}
public long getItem() {
return item;
}
}