Uploading a file in a project using GWT/EXT-GWT can be done in 2 ways : creating a separate servlet or using a library like the one presented here. Normally, because of browser security restrictions you can’t upload a file using RPC. But normally doesn’t mean never. This article will show you how to upload files using RPC in GWT projects.
Every now and then you might encounter a situation where you need to upload a file, but it would be very convenient to be able to use a RPC(Remote Procedure Call) to do that because you would like to perform some additional operations with that file.Well not to worry, this article will show you a way to do that.
We are going to start with the RPC implementation. First of all, we need to declare the RPC interfaces, this is no different from implementing an usual RPC. We are going to name our RPC “FileService”, and bellow you have the declaration of both interfaces
1. The FileService interface
@RemoteServiceRelativePath("GWTWebCbr3") public interface FileService extends RemoteService { /** * Method that stores a file on the server. It computes the save path * depending on the case type and the case id. */ public String uploadAttachement(String caseId, String fieldName,boolean isNewCase); /** * Method that deletes a file from the server. It computes the file path * depending on the case type and the case id. */ public boolean deleteAttachement(String filePath, int caseID, String fieldName); /** * Update the name of the attachment */ public String updateFileName(final String name); } |
2. The FileServiceAsync interface
public interface FileServiceAsync { /** * Method that stores a file on the server. It computes the save path * depending on the case type and the case id. */ void uploadAttachement(String caseId, String id, boolean isNewCase, AsyncCallback callback); /** * Method that deletes a file from the server. It computes the file path * depending on the case type and the case id. */ void deleteAttachement(String filePath, int id, String id2, AsyncCallback callback); /** * Update the name of the attachment */ void updateFileName(String name, AsyncCallback callback); } |
So far there is nothing different, but the service implementation will reveal what makes uploading a file with RPC possible. Bassically you need to overwrite the service(final HttpServletRequest request,HttpServletResponse response) method and that’s it.
Here’s the code for FileServiceImpl
public class FileServiceImpl extends RemoteServiceServlet implements FileService { //code omitted private FileItem uploadedFileItem; @Override protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultiPart = ServletFileUpload .isMultipartContent(new ServletRequestContext(request)); if(isMultiPart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { @SuppressWarnings("unchecked") List items = upload.parseRequest(request); uploadedFileItem = items.get(0); // we only upload one file if(uploadedFileItem == null) { super.service(request, response); return; } else if(uploadedFileItem.getFieldName().equalsIgnoreCase( "uploadFormElement")) { fileName = uploadedFileItem.getName(); response.setStatus(HttpServletResponse.SC_CREATED); response.getWriter().print("OK"); response.flushBuffer(); } } catch(FileUploadException e) { LOG.error(e); } } else { super.service(request, response); return; } } //code omitted } |
After you implement your service, you need to register it and you do that by adding the following lines to the web.xml file.
<servlet> <servlet-name>FileService</servlet-name> <servlet-class>ro.ropardo.cbr.server.Services.FileServiceImpl</servlet-class> </servlet> <servlet-mapping> <servlet-name>FileService</servlet-name> <url-pattern>/gwtwebcbr/GWTWebCbr3</url-pattern> </servlet-mapping> |
On the client side you need to use a FormPanel to upload your file. To tie the components together you need to set the action of the FormPanel to the RPC servlet. Here is an example :
private final FormPanel formPanel; formPanel = new FormPanel(); formPanel.setAction(GWT.getModuleBaseURL()+”GWTWebCbr3”); formPanel.setEncoding(FormPanel.ENCODING_MULTIPART); formPanel.setMethod(FormPanel.METHOD_POST); |
Now all you have to do is submit the form. For example you can submit the form when a user clicks a button :
Button uploadButton = new Button(PARAM_BUTTON_UPLOAD); uploadButton.addClickHandler(new ClickHandler() { @Override public void onClick(final ClickEvent event) { if(!uploader.getFilename().equalsIgnoreCase(PARAM_EMPTY_STRING)) { formPanel.submit(); } } }); |
You can also add a handler to the form and do some action when the submit completed. For example, I trigger an event if the submit completed and the file was succesfully uploaded. To check if the upload was successful you need to write the status of the operation in the result object from the overriden service() method and then, from the client side, check that status. Here is an example :
formPanel .addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() { @Override public void onSubmitComplete(final SubmitCompleteEvent event) { // call upload method from service with the file's name String result = event.getResults(); if(result.contains(PARAM_OK_STRING)) { String name = uploader.getFilename(); // trigger some event that custom file editor will FileReadyForUploadEvent ev = new FileReadyForUploadEvent(name); fireEvent(ev); } } }); |
And that is how you can upload a file using the RPC mechanism in GWT based projects. I hope you can find this article usefull.
Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.