i'm a newer. but i dont think using gwt-rpc-call can upload a file.
use servlet is a solution.
here is part of the server code(servlet),and some apache package is
needed:
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
i hope it can help
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {
int BUFFER_SIZE = 1024;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
upload(request, response);
}
@SuppressWarnings("unchecked")
protected void upload(HttpServletRequest request,
HttpServletResponse response) {
if (!ServletFileUpload.isMultipartContent(request))
return;
ServletFileUpload servletFileUpload = new ServletFileUpload(
new DiskFileItemFactory());
List fileItemsList;
try {
fileItemsList = servletFileUpload.parseRequest(request);
String optionalFileName = "";
FileItem fileItem = null;
Iterator it = fileItemsList.iterator();
while (it.hasNext()) {
FileItem fileItemTemp = (FileItem) it.next();
if (fileItemTemp.isFormField()) {
if
("filename".equals(fileItemTemp.getFieldName()
.toLowerCase()))
optionalFileName =
fileItemTemp.getString();
} else
fileItem = fileItemTemp;
}
if (fileItem == null)
return;
String fileName = fileItem.getName();
/* Save the uploaded file if its size is greater than
0. */
if (fileItem.getSize() <= 0)
return;
if (fileName == null || "".equals(fileName.trim())) {
if (optionalFileName.trim().equals(""))
fileName =
FilenameUtils.getName(fileName);
else
fileName = optionalFileName;
}
//get a utf8 filename here
fileName = new String(fileName.getBytes(System
.getProperty("file.encoding")),
"utf-8");
//write a file to temp dir
String dir = System.getenv("TEMP");
String fullPath = dir + "/" + fileName;
fullPath = fullPath.replaceAll("\\\\", "/");
File saveTo = new File(fullPath);
try {
fileItem.write(saveTo);
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("some response code");
return;
} catch (Exception e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, e
.getLocalizedMessage());
return;
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
On 4月21日, 下午12时51分, KK <[email protected]> wrote:
> Hi,
>
> Currently I cannot get the FileUpload widget to work correctly.
>
> I am trying to send and receive an image file from the server to the
> client side.
> In order to store an image uploaded by a user in my database.
>
> I can do this very easy with Java, but when using GWT it doesn't work
> the same.
>
> I'm just not sure how to get this to work, just tried passing the
> filename using RPC calls but this didn't work.
>
> Any suggestions/example will be very very helpful?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Web Toolkit" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/google-web-toolkit?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.