Jakarta fileupload servlet not working!

49 views
Skip to first unread message

Andrewww

unread,
Jun 5, 2007, 3:25:08 AM6/5/07
to Google Web Toolkit
I have a form with only one element within: a fileUpload module. I set
up a servlet using the famous example from the book "gwt in action"
and I put the correct row in module.xml file.
When I try to submit the form, I haven't any error or warning, but
simply I haven't any uploaded file on the server!:S Any explanation?

<servlet path="/fileupload"
class="com.mycompany.project.server.UploadServlet"/>

and this is the code:

package com.mycompany.project.server;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

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;

public class UploadServlet extends HttpServlet {

private static final long serialVersionUID =
2903950292836254376L;

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

if(!ServletFileUpload.isMultipartContent(request))
return;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new
ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
e1.printStackTrace();
return;
}
for(Iterator i = items.iterator(); i.hasNext();) {
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(filename);
item.write(uploadedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Steiner Dominik, (QX59311)

unread,
Jun 5, 2007, 3:30:25 AM6/5/07
to Google-We...@googlegroups.com
Hi Andrewww,

You might have a look at this thread

http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/d7da2ff9fbbe0348/6ed2f32abe5aa758?lnk=gst&q=dominik+fileupload&rnum=2#6ed2f32abe5aa758

which will hopefully help you.

Are you sure that your server method is being called? You might put some traces in your server-side code to make sure.
Did you try to debug your server code if you are sure that the server is being called?
Try to narrow the cause of the problem. If you don't see that your server is being called, try the hints from the post above and make a simple HTTPRequest to see if it gets to the server.
If your server is being called, step through the server code to see where it goes amiss.

HTH

Dominik

-----Ursprüngliche Nachricht-----
Von: Google-We...@googlegroups.com [mailto:Google-We...@googlegroups.com] Im Auftrag von Andrewww
Gesendet: Dienstag, 5. Juni 2007 09:25
An: Google Web Toolkit
Betreff: Jakarta fileupload servlet not working!

Andrewww

unread,
Jun 5, 2007, 5:19:34 AM6/5/07
to Google Web Toolkit
Hi Dominik,
yes I verified that the server is being called. It seems the servlet
interrupt itself on the instruction items =
upload.parseRequest(request); but I don't understand why....

Steiner Dominik, (QX59311)

unread,
Jun 5, 2007, 6:13:53 AM6/5/07
to Google-We...@googlegroups.com
Hi Andrewww,

I tested the code you posted. Works ok for me. I used the
FancyFileUploadWidget of Adam.

http://groups.google.com/group/Google-Web-Toolkit/msg/8a4439de9cd48006?&
q=fileupload+adam

And the server side code that you posted. Could it be that you are
trying to transfer large files?

Dominik

Andrewww

unread,
Jun 5, 2007, 12:19:40 PM6/5/07
to Google Web Toolkit
No, I'm not transfering large files.
I tried Adam's widget, but it didn't work.....I have the "Operation
failed" message

Andrewww

unread,
Jun 5, 2007, 12:31:42 PM6/5/07
to Google Web Toolkit
Don't you have a 100% working simple code? I don't need that
complicated Adam's code...

Adam T

unread,
Jun 5, 2007, 12:45:08 PM6/5/07
to Google Web Toolkit
Andrewww, there's a bit of a bug in my code for the FancyFileUpload
that sometimes gives Operation Failed even if its been successful -
but if Dominik got it working with your server side code, then I guess
you are looking for some other explanation.

Might be a dumb question, but where exactly are you expecting your
file to be stored once it is uploaded - have you tried explicitly
setting an upload directory? Perhaps using:

String rootDirectory = "some known directory"
File uploadedFile = new File(rootDirectory+fileName);

There is also the Apache IO package (http://jakarta.apache.org/commons/
io/) that contains some useful file utilities that you can use to get
just the filename directly instead of the slash indexing you are
using, e.g:

import org.apache.commons.io.FilenameUtils;

String fileName =
item.getName();
if (fileName != null && !
fileName.equals("")) {
fileName =
FilenameUtils.getName(fileName);
File uploadedFile =
new File(rootDirectory+fileName);
try {

item.write(uploadedFile);

out.print(returnOKMessage);
} catch (Exception
e) {

e.printStackTrace();
}
}

Hope some of that helps!

//Adam

Andrewww

unread,
Jun 5, 2007, 1:14:00 PM6/5/07
to Google Web Toolkit
What that out and printOKMessage() are? In my server-side code I have
nothing similar to this!

Andrewww

unread,
Jun 5, 2007, 1:18:20 PM6/5/07
to Google Web Toolkit
Ok...
I added in my server side code those missing lines and now it is like
this:

package com.mycompany.project.server;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

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;

public class UploadServlet extends HttpServlet {

private static final long serialVersionUID = 2903950292836254376L;

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

if(!ServletFileUpload.isMultipartContent(request))
return;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
e1.printStackTrace();
return;
}

PrintWriter out = response.getWriter();


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

if(!item.isFormField()) {
String filename = item.getName();
if(filename!=null && filename!="") {
filename = FilenameUtils.getName(filename);


File uploadedFile = new File(filename);

try {
item.write(uploadedFile);
out.print("OK");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}

But it still gives me "Operation failed"

Adam T

unread,
Jun 5, 2007, 1:20:25 PM6/5/07
to Google Web Toolkit
Its just used to return a message to the caller, earlier on in my code
I have the line:

PrintWriter out = response.getWriter();

So with the line

out.print(returnOKMessage);

the code is saying print the constant returnOKMessage to the response
part of the servlet.

(I just took the code from the server side of the Fancy File Upload
Widget - scroll down to the third message here
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/19ea5c6be6d47848/8a4439de9cd48006?#8a4439de9cd48006)

//Adam

Adam T

unread,
Jun 5, 2007, 1:28:05 PM6/5/07
to Google Web Toolkit
As I mentioned, there is a potential bug in the FancyFileUpload code
if you've just copied from the link.

In your code above it is still not exactly clear where you are
expecting to store the file (so it could have succeeded and you may be
looking in the wrong place for it). Try explicitly setting the
directory name, e.g.

File uploadedFile = new File("/home/youname/"+fileName);

or

File uploadedFile = new File("C:\TempUpload\"+fileName);

then check in that directory. The only other advice I have is to use
an IDE in which you can debug line by line both client and server side
to see where you are getting a problem.

//Adam

Adam T

unread,
Jun 5, 2007, 1:31:29 PM6/5/07
to Google Web Toolkit
also are you sure all of the necessary jakarta commons packages are in
the classpath of your hosted mode (io, fileupload and I seem to
remember I needed codec too)?

Andrewww

unread,
Jun 6, 2007, 3:33:45 AM6/6/07
to Google Web Toolkit
Thanks a lot Adam. Now it's working, also if I always have the
"Operation Failed" problem...
Maybe I try to debug and found the source of the issue...

Andrewww

unread,
Jun 6, 2007, 4:03:28 AM6/6/07
to Google Web Toolkit
I found the problem: I think it's is due to some differences between
Linux and Windows systems, or GWT's Windows and Linux versions. In
fact I use Linux, and the value of event.getResults().toString() is
"<pre>OK</pre>".
Once discovered this fact, I added some lines to parse this message
and now the code is fully working ;)

Thanks a lot, Adam

Rocco

unread,
Jun 12, 2007, 7:54:16 PM6/12/07
to Google Web Toolkit
Hi Andrewww,

I used the same code as you did from the book "GWT in Action", it
seems
to throw exception on the code, "items =
upload.parseRequest(request);",
which you mentioned in the beginning.

May I know how to fix this?

Thanks.
Rocco

Rocco

unread,
Jun 12, 2007, 8:23:51 PM6/12/07
to Google Web Toolkit
I got the following exception from Servlet.service() in the server:

[WARN] StandardWrapperValve[shell]: Servlet.service() for servlet
shell threw exception
java.lang.NoClassDefFoundError: org/apache/commons/io/output/
DeferredFileOutputStream
at
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:
191)
at
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:
350)
at
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:
126)
at
com.skyrider.sadmin.server.UploadServlet.service(UploadServlet.java:
46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
com.google.gwt.dev.shell.GWTShellServlet.service(GWTShellServlet.java:
249)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
237)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
157)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
214)

Rocco

unread,
Jun 12, 2007, 8:58:32 PM6/12/07
to Google Web Toolkit
The problem seems to be gone after I downloaded apache's commons.io
jar file.

Rocco

unread,
Jun 20, 2007, 10:28:28 AM6/20/07
to Google Web Toolkit, andre...@gmail.com
Hi Andrewww85,

I also had the result of "<pre>...</pre>" when calling
"event.getResults()".
And I was running in Web Mode on Windows instead of Linux. There is no
such "<pre>...</pre>" when running in Hosted mode though.

The version of GWT I used was 1.3.3.

Thanks.
Rocco

Reply all
Reply to author
Forward
0 new messages