Thank you for your help. Combined with the website I mentioned earlier
(which was incomplete and does not work exactly as described, though
it is close), I have figured out how to do this. Here are the complete
(I think) instructions for anyone else attempting to do this:
Obtain and install
- GWT (I have version 1.4)
- Eclipse (I have version 3.3)
- Cypal Studio for GWT (I have version RC5)
- The Eclipse plugins needed for Cypal (WPT and some others, I think)
- commons-fileupload and commons-io (as described here:
http://home.izforge.com/index.php/2006/10/29/295-handling-file-uploads-with-the-google-web-toolkit)
In Eclipse, go to Window->Preferences->Cypal Studio and set the GWT
home to be the directory where you installed GWT.
Create a new Dynamic Web Project named UploadFile.
Right-click on the project and go to Properties->Java Build Path-
>Libraries->Add External Jars. Add the files commons-fileupload.jar
and commons-io.jar, which should be contained in the download for each
of those libraries.
Right-click on the project and select New Cypal Studio Module. Name
the module UploadFile, and put it in the package uploadfile.
Right-click on the project and select New Servlet. Name the servlet
UploadFileServlet, and put it in the package uploadfile.
The previous two steps take care of adjusting *most* of the proper xml
files for you, but to work with GWT in hosted mode, you must add the
line
<servlet path="/UploadFileServlet"
class="uploadfile.server.UploadFileServlet"/>
inside the module tag of the file UploadFile.gwt.xml.
Paste the following code into UploadFile.java:
package uploadfile.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
public class UploadFile implements EntryPoint {
public void onModuleLoad() {
// Create a FormPanel and point it at a service.
final FormPanel uploadForm = new FormPanel();
uploadForm.setAction(GWT.getModuleBaseURL() +
"UploadFileServlet");
// Because we're going to add a FileUpload widget, we'll need to
set the
// form to use the POST method, and multipart MIME encoding.
uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
uploadForm.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
uploadForm.setWidget(panel);
// Create a FileUpload widget.
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
// Add a 'submit' button.
Button uploadSubmitButton = new Button("Submit");
panel.add(uploadSubmitButton);
uploadSubmitButton.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
uploadForm.submit();
}
});
uploadForm.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) {
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
Window.alert(event.getResults());
}
});
RootPanel.get().add(uploadForm);
}
}
This is essentially the example from the javadoc for the FileUpload
widget, with the setAction method (Jason: setTarget does not appear to
be a method in FormPanel; perhaps you are thinking of another class?)
corrected to erase a warning you will get in hosted mode, and with the
setName method called on the FileUpload object in order to allow the
servlet later to identify the FileUpload object.
Paste the following code into UploadFileServlet:
package uploadfile.server;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class for Servlet: UploadFileServlet
*/
public class UploadFileServlet extends HttpServlet implements Servlet
{
private static final long serialVersionUID = 8305367618713715640L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
FileItem uploadItem = getFileItem(request);
if (uploadItem == null) {
response.getWriter().write("NO-SCRIPT-DATA");
return;
}
byte[] fileContents = uploadItem.get();
//TODO: add code to process file contents here. We will just print
it.
System.out.println(new String(fileContents));
response.getWriter().write(new String(fileContents));
}
private FileItem getFileItem(HttpServletRequest request) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()
&& "uploadFormElement".equals(item.getFieldName())) {
return item;
}
}
} catch (FileUploadException e) {
return null;
}
return null;
}
}
This is essentially the code from
http://home.izforge.com/index.php/2006/10/29/295-handling-file-uploads-with-the-google-web-toolkit,
but with the expression
"uploadFormElement".equals(item.getFieldName()) altered to be
consistent with the line upload.setName("uploadFormElement").
If you run this from Eclipse in hosted mode and upload a text file,
you should see the contents of the file in a pop-up window (between
pre tags for some reason), and the servlet will print the contents of
the file to the standard output console in Eclipse, which corresponds
to the two statements after the //TODO comment.
Dave