<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();
}
}
}
You might have a look at this thread
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!
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
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
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"
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
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
Thanks a lot, Adam
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
[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)
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