Monday, February 28, 2011

Amazon S3 File Upload In Java - Google Web ToolKit

Learn How to Upload Files to amazon s3 using Google web toolkit .


Client Side Coding ..

Create FormPanel on client Side page.

FormPanel formPanel=new FormPanel();

FormPanel allows you to set action and encoding method like we do in HTML Forms .

If you are using GWT FormPanel then use this

formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);

If you are using EXT GWT FormPanel then use this

formPanel.setEncoding(Encoding.MULTIPART);
formPanel.setMethod(Method.POST);


Write a listener to your upload button and write the below code under the button listener.


String fileName = fileUploadField.getValue();
String fileType = fileUploadField.getValue();
fileType = fileType.substring(fileType.indexOf(".") + 1);

if (fileName != null && !fileName.equalsIgnoreCase(""))
{
amazonFileUploadServletUrl = GWT.getHostPageBaseURL()+ "amazonfileuploadservlet?bucketName=" + "BukcetName"
+ "&keyName=" + "BukcetNameKey" + "&fileName="+ fileName + "&fileType=" + fileType;
System.out.println("amazonFileUploadServletUrl="+ amazonFileUploadServletUrl);
}
formPanel.setAction(amazonFileUploadServletUrl);
formPanel.submit();



Server side coding..

Get the AmazonFileUploadServlet.java and S3Upload.java and put it in server package.

Download aws-java-sdk.jar ,common-codec.jar,common-fileupload.jar,commons-io.jar and common-httpclient.jar and put it in WebProject > War > Web-Inf > Lib >Paste It all jar files.


You have done the client side coding as well as server side coding, now you have to make a communication between client and server using servlet mapping

Go to Web.xml and write servlet path and servlet mapping

AmazonFileUploadServlet
your server package


AmazonFileUploadServlet
/amazonfileuploadservlet
Now compile and run your project to upload the files to amazon s3.


For Further Reading,
Amazon, Java

0 comments:

Post a Comment