The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

odejonge (4) [Avatar] Offline
#1
I try to implement the FileUpload widget and the UploadServlet as described in the book, everything goes well till I come to the line in the UploadServlet that gets the items.

items = upload.parseRequest(request);

It does not give an exception, it just quits and ends up in the org.apache.catelina.core.ApplicationFilterChain

/**CLIENT**/
form.setMethod(FormPanel.METHOD_POST); form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setAction(GWT.getModuleBaseURL() + "upload");
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) {
if (fileUpload.getFilename().length() == 0){
Window.alert("Je moet een file selecteren");
event.setCancelled(true);
return;
}
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
String result = event.getResults();
if(result.equals("success")) {
Window.alert("File upload gelukt!");
}else{
Window.alert("Sorry kon file niet uploaden");
}
}
});
form.setWidget(formPanel);
formPanel.add(fileUpload);
fileUpload.setName("file");
formPanel.add(submit);
submit.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
form.submit();
}});

/***SERVLET**/
public class UploadServlet extends HttpServlet {

private static final long serialVersionUID = 111L;

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

super.service(request, response);
if( !ServletFileUpload.isMultipartContent(request))
return;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

List items = null;
try {
//I get stuck here, it doesn't throw an exception but just quits
items = upload.parseRequest(request);
}catch (FileUploadException e) {
e.printStackTrace();
return;
}catch(Exception e){
e.printStackTrace();
return;
}

try{
for (Iterator i = items.iterator(); i.hasNext()smilie {
FileItem item = (FileItem) i.next();

if(item.isFormField())
continue;

String fileName = item.getName();
int slash = fileName.lastIndexOf("/");
if(slash == -1){
slash = fileName.lastIndexOf("\");
}
if(slash != -1){
fileName = fileName.substring(slash + 1);
}
try{
File uploadedFile = new File("/uploads/" + fileName);
item.write(uploadedFile);
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e) {
e.printStackTrace();
return;
}

}

}
odejonge (4) [Avatar] Offline
#2
Re: Upload File
Solved problem:
Had not included in my project the commons-io-1.4.jar's...
robert.hanson (173) [Avatar] Offline
#3
Re: Upload File
That would do it smilie Glad to hear that you got it working.
odejonge (4) [Avatar] Offline
#4
Re: Upload File
Hi Robert,

Thanks for your reaction, but there is another problem with the servlet I can't solve:

It's giving me 405 http status message back: Method not allowed. You know, by any chance the reason of this? (Again a missing library?)
robert.hanson (173) [Avatar] Offline
#5
Re: Upload File
Make sure that you are using the right HTTP method in your client form. It must be POST. The error is indicating that it is using an HTTP method that is not enabled.
odejonge (4) [Avatar] Offline
#6
Re: Upload File
Hi robert,

As you can see in the code I set the method to POST and encoding to MULTIPART
form.setMethod(FormPanel.METHOD_POST); form.setEncoding(FormPanel.ENCODING_MULTIPART);

I tried in the servlet beside from the service() also the doPost() method. This doesn't help me any further. Are there any other circumstances influencing this problem
robert.hanson (173) [Avatar] Offline
#7
Re: Upload File
I'm not sure why you are getting that, it looks like everything is right. Are you doing this in hosted mode, or deployed to a server? In either case, you might want to post the details to the GWT forum, if it is a common problem it is likely someone there will have an immediate answer.
derekypp (1) [Avatar] Offline
#8
Re: Upload File
Hi to everyone

I copy odejonge 's code to my project ,
but i get exception is " java.lang.NullPointerException "


public void onSubmitComplete(final FormSubmitCompleteEvent event) {
String result = event.getResults();
if(result.equals("success")) {
Window.alert("File upload gelukt!");
}else{
Window.alert("Sorry kon file niet uploaden");
}
}
}
});

Becose event is NULL


everyone can help me
many thanks to all