How to use GWT rpc to upload an image

563 views
Skip to first unread message

victor QIN

unread,
May 6, 2010, 7:46:20 AM5/6/10
to Google Web Toolkit
Hello,

I want to know how to use gwt rpc to upload an image. I have tried
to use ordinary HttpServlet and It works. but for the servlet that
extends RemoteServiceServlet , I don't know how to do it. the codes
are following:
VerticalPanel holder = new VerticalPanel();
final MyFileUpload upload = new MyFileUpload();
final FormPanel form = new FormPanel();
Button button = new Button("submit");
holder.add(upload);
holder.add(button);
form.add(holder);
upload.setName("GWTServlet");

holder.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);

form.setWidth("275px");
// form.setAction(GWT.getModuleBaseURL() + "GWT");



form.addFormHandler(new FormHandler() {

public void onSubmit(FormSubmitEvent event) {

getService().UploadImage(new AsyncCallback<Void>() {

public void onFailure(Throwable caught) {
Window.alert("failed");
}

public void onSuccess(Void result) {
Window.alert("success");
}
});
event.setCancelled(false);
}

public void onSubmitComplete(FormSubmitCompleteEvent
event) {
String result = event.getResults();
if (result != null) {
result = result.replaceAll("<pre>", "");
result = result.replaceAll("<\\\\pre>", "");
result = result.replaceAll("</pre>", "");
Window.alert(result);
}
}
});
button.addClickHandler(new ClickHandler() {

public void onClick(ClickEvent event) {
form.submit();

}
});
in the servlet extending from RemoteServiceServlet
public void UploadImage(MyFileUpload file) {
HttpServletRequest request = getThreadLocalRequest();
HttpServletResponse response =getThreadLocalResponse();
// response.setContentType("text/x-gwt-rpc");
boolean isMultipart =
ServletFileUpload.isMultipartContent(request);


boolean writeToFile = true;


String classPath =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String webPath = "";
if (classPath.indexOf("web") > 0) {
classPath = classPath.substring(0,
classPath.indexOf("web/") + 4);

if (classPath.indexOf("build") > 0) {
webPath = classPath.substring(1,
classPath.indexOf("build")) +
classPath.substring(classPath.indexOf("build") + 6,
classPath.length());
}
//out.println(webPath);
//
out.println(Upload.class.getClassLoader().getResource(""));
}
String rootDirectory = webPath + "images/";
if (isMultipart) {

FileItemFactory factory = new DiskFileItemFactory();

// Create a new file Upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
try {
List items = upload.parseRequest(request);

if (items.size() == 0) {
try {
//out.print("sdf");
} catch (Exception e) {
e.printStackTrace();
// out.print("Error:" + e.getMessage());
}
return;
}

..............
}
Like this. but it has content-type error. What should I do? thank you
very much

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Sripathi Krishnan

unread,
May 6, 2010, 2:07:03 PM5/6/10
to google-we...@googlegroups.com
You can't use RPC or Ajax to upload a file, you have to use a regular form (or FormPanel)

On the server side, RemoteServiceServlet expects the request to be a RPC request, and hence won't work if you try to upload a file. You would have to use a regular HttpServlet to get uploads working.

Also, a lot of people have had success with the gwt-upload library. You may want to use it instead of writing the upload code yourself.

--Sri
Reply all
Reply to author
Forward
0 new messages