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.