File uploading program is a combination of client and server sub programs.
There are some jar files which is neccessary to make this project.
Requirements:
1. Commons-fileupload-1.2.1
Download- Commons-fileupload-1.2.1.jar
2. Commons-io-1.4
Download- Commons-io-1.4.jar
3. Missing plugins for your browser
Download- missing plugins
Step1:-
First you create a new project. The name of this project is FileUploadExample.
Step2:-
Right click on left side-->Import-->General-->Existing Projects into workspace-->next-->Browse the FileUploadExample-->Finish
Step3:-
Add a composite in com.client
Right click on com.client-->Google Web Toolkit-->Composite-->Name(FileUploadDesign)-->Finish
and paste this code into this file.
FileUploadDesign.java
package com.client;
import com.google.gwt.core.client.GWT;
import com.gwtext.client.core.Connection;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.FormListenerAdapter;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
public class FileUploadDesign extends Window
{
FormPanel formPanel = new FormPanel();
Grid grid = new Grid(1,1);
public FileUploadDesign()
{
super("Photo Upload");
setSize("500px", "400px");
formPanel.setFileUpload(true);
//setup error reader to process from submit response from server
RecordDef errorRecordDef = new RecordDef(new FieldDef[]
{
new StringFieldDef("id"),new StringFieldDef("msg")
});
XmlReader errorReader = new XmlReader("field", errorRecordDef);
errorReader.setSuccess("@success");
formPanel.setErrorReader(errorReader);
final TextField textField = new TextField("Photo", "file");
textField.setInputType("file");
textField.setSize("334px", "28px");
formPanel.add(textField);
this.add(formPanel);
this.addButton(new Button("Submit",new ButtonListenerAdapter()
{
public void onClick(Button button, EventObject e)
{
MessageBox.confirm("Confirm", "Do you want to submit?",new MessageBox.ConfirmCallback()
{
public void execute(String btnID)
{
if (btnID.equals("yes"))
{
//Do not change here in this syntax
formPanel.getForm().submit(GWT.getModuleBaseURL()+"upload", null, Connection.POST, "Saving Data...", true);
}
}
});
}
}));
formPanel.addFormListener(new FormListenerAdapter()
{
public boolean doBeforeAction(Form form)
{
return true;
}
public void onActionComplete(Form form, int httpStatus, java.lang.String responseText)
{
//set here the root directory path
Image image = new Image("g:/images/"+responseText);
image.setSize("300px", "300px");
boolean b=image.isVisible();
System.out.print(b);
grid.setWidget(0, 0,image);
}
public void onActionFailed(Form form, int httpStatus, java.lang.String responseText)
{
com.google.gwt.user.client.Window.alert("File upload is failed.");
}
});
this.add(grid);
}
}
Step4:-
Now we should configure GWT-Ext
Right click on com.client-->Google Web Toolkit-->Configure for using GWT-Ext
Step5:-Add a Server page on com.server. Here Servlet is working as fileuploading server.
so create a class at server side and delete all code and then paste the blow code.
Right click on com.server-->New-->Class-->Name(FileUploadServlet)-->Finish
FileUploadServlet.java
There are some jar files which is neccessary to make this project.
Requirements:
1. Commons-fileupload-1.2.1
Download- Commons-fileupload-1.2.1.jar
2. Commons-io-1.4
Download- Commons-io-1.4.jar
3. Missing plugins for your browser
Download- missing plugins
Step1:-
First you create a new project. The name of this project is FileUploadExample.
Step2:-
Right click on left side-->Import-->General-->Existing Projects into workspace-->next-->Browse the FileUploadExample-->Finish
Step3:-
Add a composite in com.client
Right click on com.client-->Google Web Toolkit-->Composite-->Name(FileUploadDesign)-->Finish
and paste this code into this file.
FileUploadDesign.java
package com.client;
import com.google.gwt.core.client.GWT;
import com.gwtext.client.core.Connection;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.data.XmlReader;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.FormListenerAdapter;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
public class FileUploadDesign extends Window
{
FormPanel formPanel = new FormPanel();
Grid grid = new Grid(1,1);
public FileUploadDesign()
{
super("Photo Upload");
setSize("500px", "400px");
formPanel.setFileUpload(true);
//setup error reader to process from submit response from server
RecordDef errorRecordDef = new RecordDef(new FieldDef[]
{
new StringFieldDef("id"),new StringFieldDef("msg")
});
XmlReader errorReader = new XmlReader("field", errorRecordDef);
errorReader.setSuccess("@success");
formPanel.setErrorReader(errorReader);
final TextField textField = new TextField("Photo", "file");
textField.setInputType("file");
textField.setSize("334px", "28px");
formPanel.add(textField);
this.add(formPanel);
this.addButton(new Button("Submit",new ButtonListenerAdapter()
{
public void onClick(Button button, EventObject e)
{
MessageBox.confirm("Confirm", "Do you want to submit?",new MessageBox.ConfirmCallback()
{
public void execute(String btnID)
{
if (btnID.equals("yes"))
{
//Do not change here in this syntax
formPanel.getForm().submit(GWT.getModuleBaseURL()+"upload", null, Connection.POST, "Saving Data...", true);
}
}
});
}
}));
formPanel.addFormListener(new FormListenerAdapter()
{
public boolean doBeforeAction(Form form)
{
return true;
}
public void onActionComplete(Form form, int httpStatus, java.lang.String responseText)
{
//set here the root directory path
Image image = new Image("g:/images/"+responseText);
image.setSize("300px", "300px");
boolean b=image.isVisible();
System.out.print(b);
grid.setWidget(0, 0,image);
}
public void onActionFailed(Form form, int httpStatus, java.lang.String responseText)
{
com.google.gwt.user.client.Window.alert("File upload is failed.");
}
});
this.add(grid);
}
}
Step4:-
Now we should configure GWT-Ext
Right click on com.client-->Google Web Toolkit-->Configure for using GWT-Ext
Step5:-Add a Server page on com.server. Here Servlet is working as fileuploading server.
so create a class at server side and delete all code and then paste the blow code.
Right click on com.server-->New-->Class-->Name(FileUploadServlet)-->Finish
FileUploadServlet.java
package com.server;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
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;
import org.apache.commons.io.FilenameUtils;
import com.google.gwt.user.client.Window;
@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Set the root directory where you want to store the uploaded files
String rootDirectory = "g:/images/";
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart)
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Process the uploaded items
try {
// Parse the request
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
}
else
{
String fileName = item.getName();
if (fileName != null && !fileName.equals(""))
{
fileName = FilenameUtils.getName(fileName);
File uploadedFile = new File(rootDirectory + fileName);
try {
item.write(uploadedFile);
//here set the root directory path
File newfile=new File("g://gwt-windows-1.7.1//logcom//war//images//"+uploadedFile.getName()+".jpg");
uploadedFile.renameTo(newfile);
response.getWriter().write(newfile.getName());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
}
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
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;
import org.apache.commons.io.FilenameUtils;
import com.google.gwt.user.client.Window;
@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//Set the root directory where you want to store the uploaded files
String rootDirectory = "g:/images/";
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart)
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Process the uploaded items
try {
// Parse the request
List items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
}
else
{
String fileName = item.getName();
if (fileName != null && !fileName.equals(""))
{
fileName = FilenameUtils.getName(fileName);
File uploadedFile = new File(rootDirectory + fileName);
try {
item.write(uploadedFile);
//here set the root directory path
File newfile=new File("g://gwt-windows-1.7.1//logcom//war//images//"+uploadedFile.getName()+".jpg");
uploadedFile.renameTo(newfile);
response.getWriter().write(newfile.getName());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
}
}
}
}
Step6:-
Add External jar
Now we need to add jar file in Eclipse IDE. And command to add jar file is giving blow.
Now we need to add jar file in Eclipse IDE. And command to add jar file is giving blow.
Right click on main FileUploadExample-->Build Path-->Add External Archives-->select the jar file
Step7:-
Now edit the Web.xml
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Servlets -->
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/fileuploadexample/upload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>FileUploadExample.html</welcome-file>
</welcome-file-list>
<!-- Servlets -->
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/fileuploadexample/upload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>FileUploadExample.html</welcome-file>
</welcome-file-list>
</web-app>
Now you run this project You will able to upload files.
If you have any problem then you can mail us to contact our gwt team Miss Sonal Garg and Mr. Vishal Dixit.
14 July 2019 at 08:03
Can we upload image too using this?