yes thad i am using a real directory on my computer.
-- this is my entry point
public class UploadFile implements EntryPoint {
private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL()
+ "upload";
private static final String SERVER_ERROR = "An error occurred while "
+ "attempting to contact the server. Please check your network "
+ "connection and try again.";
public void onModuleLoad() {
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction(UPLOAD_ACTION_URL);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
ListBox lb = new ListBox();
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
panel.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
}));
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
Window.alert(event.getResults());
}
});
RootPanel.get().add(form);
}
}
this is my servlet
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String UPLOAD_DIRECTORY = "C:\\Users\\dell\\Downloads\\";
private static final String DEFAULT_TEMP_DIR = "upload";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// process only multipart requests
if (ServletFileUpload.isMultipartContent(req)) {
File tempDir = getTempDir();
if (!tempDir.exists()) {
tempDir.mkdirs();
}
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
try {
List<FileItem> items = upload.parseRequest(req);
for (FileItem fileItem : items) {
// process only file upload
if (fileItem.isFormField()) continue;
String fileName = fileItem.getName();
// get only the file name not whole path
if (fileName != null) {
fileName = FilenameUtils. getName(fileName);
}
File uploadedFile = File.createTempFile(UPLOAD_DIRECTORY,fileName);
if (uploadedFile.createNewFile()) {
fileItem.write(uploadedFile);
resp.setStatus(HttpServletResponse.SC_CREATED);
resp.getWriter().print("The file was created successfully.");
resp.flushBuffer();
} else
throw new IOException("The file already exists in repository.");
}
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"An error occurred while creating the file : " + e.getMessage());
}
} else {
resp.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE,
"Request contents type is not supported by the servlet.");
}
}
private File getTempDir() {
return new File(DEFAULT_TEMP_DIR);
}
}
an finally my web.xml
<servlet>
<servlet-name>uploadServlet</servlet-name>
<servlet-class>com.gwt.upload.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadServlet</servlet-name>
<url-pattern>/uploadfile/upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>UploadFile.html</welcome-file>
</welcome-file-list>
</web-app>
and now i am getting this error
Le jeudi 3 avril 2014 00:36:28 UTC+2, imen boukhris a écrit :
Le jeudi 3 avril 2014 00:36:28 UTC+2, imen boukhris a écrit :
Hi GWT community,how can i upload a file to server from my desktop in my google web toolkit project.Anyone could provide me with good tutorial that explain the file upload solution from start to end please.thanks in advance
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment